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++
Write a Python program that will ask the user to input a word, will return the...
Write a Python program that will ask the user to input a word, will return the first letter of the input word, and ask the user to put another word, and so on, in the form of a loop. If the user chooses to stop, he or she should input the integer "0" for the loop to stop.
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...
1. Write a Python program to: ask the user to enter two integers: int1 and int2....
1. Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative...
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.
Write a python program that will ask the user to enter any number of days. The...
Write a python program that will ask the user to enter any number of days. The program reads the number of days from the user, and returns how many years, months, and days are there in the given number of days. Your program should prompt the user to: Enter the number of days : 1152 The user types number of days (1152 above, for example) and hit enter key. The program then will print: 3 years 1 months 27 days...
Write a python program that will ask the user to enter as many positive and negative...
Write a python program that will ask the user to enter as many positive and negative numbers and this process will only stop when the last number entered is -999. Use a while loop for this purpose. Your program should compute three sums; the sum of all numbers, sum of all positive numbers, and the sum of all negative numbers and display them. It should also display the count of all numbers, count of all positive numbers, and count of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT