How to Find Area of Triangle using C++ Programming Language

Area of Triangle:

The area of Triangle is equal to multiplication of length of base , height and constant value equal to 0.5.
A = .5 * b * h
where
b ==> denotes length of base 
h ==> denotes length of height
0.5 ==> constant value

Explanation of Program code:

Lets talk about how to write a program code for area of triangle using C++ programming language.First thing you need is to enter the library files for input and output streams.By input and output streams i mean cin and cout respectively.The library file for them is #include<iostream> or #include<iostream.h> .

Then you have to enter the main function which is basic part of every program in C++. For all the programs you need this program. int main(){} is syntax of this program.int is data type function named as main.Inside the curly braces{} you have to write the program code. Return statement you have to write at the end of program code but inside the curly braces of main function as return 0 ;  .

Now look at the formula, how many variables you need and of which data type.One is to store the value of base , second is to store the value of height , third is to store the value of constant value and forth to store the value of area. All of data type double which can handle decimal values.

cout is for output statement and cin is for input statement. cout displays the value and cin gets the value and store in variable define in front of cin like cin>>b , the value is going to store in variable b of data type double.

Recommended Article: Read this article to understand why i used this loop. It is just to avoid again and again execution or compling of program code in order to solve area of different triangles.It is not necessary,if you don't want to use it remove it.
In order to remove this loop then remove the character variable which is char redo;   along with the statements after this statement 
cout<<"***************************"<<endl;
till the return statement but not the return statement.

Program code:
#include<iostream>
using namespace std;
int main()
{
double base , height , area;
 char redo;
do
{
cout<<"Enter base of Triangle "<<endl;
cin>>base;
cout<<endl;

cout<<"Enter height of Triangle "<<endl;
cin>>height;
cout<<endl<<endl;

area = 0.5 * base * height ;

cout<<"Area of Triangle is  =  "<<area;
cout<<endl;
 cout<<"*********************************"<<endl;
 cout<<"Enter y for next calculation  =  ";
 cin>>redo;
 cout<<endl;

}while(redo == 'y');
return 0;
}

Output:
Enter base of Triangle
2
Enter height of Triangle
2

Area of Triangle is = 2
*********************************
Enter y for next calculation =  

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.