+ 1

Can somebody tell me whats wrong in my code to find the area of triangle. It always show result as 0.

#include <iostream> using namespace std; int main () { int a , b; cout << "ENTER THE BASE OF TRIANGLE \n"; cin >> a; cout << "ENTER THE HIGHT OF TRIANGLE \n"; cin >> b; int sum = (1/2)*a * b; cout << sum; return 0; }

20th May 2017, 6:34 AM
Aaryan Negi
Aaryan Negi - avatar
3 odpowiedzi
+ 3
change (1/2) to (1.0/2) or (1/2.0) because (1/2) is integer division so it will return 0.
20th May 2017, 6:37 AM
Setiawan Next
Setiawan Next - avatar
+ 3
Use floats or doubles instead of int
20th May 2017, 6:43 AM
aklex
aklex - avatar
- 1
float a, b, area; area=(1/2) *a*b
20th May 2017, 2:52 PM
Aditya Singh
Aditya Singh - avatar