Question

In: Computer Science

1. Write a Python program that will ask the user length and width of the right...

1. Write a Python program that will ask the user length and width of the right triangle and find the area of the right-angled triangle. The formula for finding the area of a right-angle triangle is

ab/2. Also, find out the result if you calculate as (ab)/2. Is it the same? If it is same, why it is the same. If it is not the same, why it is not the same.

Solutions

Expert Solution

CODE FOR THE FOLLOWING PROGRAM:-

#length to store length and width to store width of triangle
length=int(input("Enter the length of the right triangle: "));
width=int(input("Enter the width of the right triangle: "));
#Calculating area
#a*b/2 - Here a is length and b is width
area=length*width/2;
#printing the output
print("The area of the triangle is: "+ str(area))

REASON FOR SAME RESULT:- a*b/2 and (a*b)/2 will generate the same result it is because in first case i.e a*b/2 because of the same precedence of multiplication and divison operator's associativity(the order of operators in which they will be evaluated) of the operators will be used to determine the result. In python most of the operators follow left to right associativity. Therefore, first multiplication will take place i.e first a*b will be evaluated and then it will be divided by 2. However, in second case i.e (a*b)/2 here the precedence of the parentheses() is greater than divison /. Therefore, first the operands and operator inside the parentheses will be evaluated which is (a*b) and then it will be divided by 2.

Hence , both will give the same result because in both cases first multiplication is done and then divison i.e the expression is evaluated in the same way though the reasons for evaluation is different in both cases.

SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-

When result is calculated as ab/2:-

When result is calculated as (ab)/2:-

HAPPY LEARNING


Related Solutions

In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
C++ Question: write a program that prompts the user for the length and width of a...
C++ Question: write a program that prompts the user for the length and width of a rectangle in inches.  The program then uses functions to compute the perimeter and area of the rectangle and to convert those to meters and square meters respectively. Sample output from one instance of the program is shown below: ```html Welcome to the Foot-To-Meter Rectangle Calculator ================================================= Enter the rectangle length in feet: 2 Enter the rectangle width in feet: 3 The rectangle dimensions are: 0.61...
ask the user for the length and width of ywo rectangles. tell the user the area...
ask the user for the length and width of ywo rectangles. tell the user the area of the reftangle with the greater width. C++
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT