Click here to Skip to main content
14,766,249 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a program that calculates the area of a right triangle. The formula is one half the height multiplied by the base. You must assign values within the program (no user input). Dimensions are real numbers, so your variables should be type float. The result should be output to the screen in the following manner.

(Note: where you see ll(LL), hh, and aa below, there should be numbers)

The area of a right triangle with base length 11 and height hh is aa

What I have tried:

#include <iostream>

using namespace std;

int main()

{

float base, height, area;

base=10.3

height=13.56;

area=1/2*base*height;

cout<<”The area of a right triangle with base 10.3″<<base<<”and height 13.56″<<height<<”is”<<area<<endl;

system(“pause”);

return 0;

}
Posted
Updated 10-May-20 13:58pm
Comments
Member 13541763 10-May-20 8:14am
   
please make is clear that you trying to display only two digits of float value?
Patrice T 10-May-20 8:18am
   
Do you see no output or wrong output ?
Member 13541763 10-May-20 8:23am
   
dear, you have not shown us output.
Patrice T 10-May-20 8:28am
   
I am not the author of question, and my comment is not for you.

The problem here is the same as in one of your former questions. 1/2 is an integer division which will yield zero. Possible working formulas:
area = 0.5 * base * height;
area = (base * height) / 2;
area = (1.0 / 2) * base * height;
   
Comments
Member 13541763 10-May-20 8:27am
   
simply convert area=1/2*base*height; to area=1/2.0*base*height;
Phil.o has the right idea. Any of those will work.

While you're at it, you should change your output line to be less redundant.
cout<<"The area of a right triangle with base "<< base <<" and height ″<< height << " is " << area << endl;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900