C++ Program to Find Area of Circle Triangle Rectangle

In this post, we will write the C++ program to find the area of Circle Triangle and Rectangle. We will develop all these programs separately.

C++ Program to Find the Area of Circle

#include<iostream>
using namespace std;
int main()
{
  float radius, area;

  cout << "Enter radius of circle: ";
  cin >> radius;

  area = 3.14*radius*radius;
  cout << "Area = " << area << endl;

  return 0;
}

Output:-

Enter radius of circle: 9.5
Area = 283.385

The area of the circle is calculated by 3.14*r2, where r is the radius. We take two variables radius and area to store the values.

Area of Circle Using Symbolic Constant

#include<iostream>
#define PI 3.14
using namespace std;
int main()
{
  float radius, area;

  cout << "Enter radius of circle: ";
  cin >> radius;

  area = PI*radius*radius;
  cout << "Area = " << area << endl;

  return 0;
}

Here we defined PI as a macro. In the program whenever PI comes, pre-processor replace it with 3.14

C++ Program to Calculate the Area of Triangle having Height and Base

#include<iostream>
using namespace std;
int main()
{
  float base, height, area;

  cout << "Enter length of base and height: ";
  cin >> base >> height;

  area = (base * height) / 2;
  cout << "Area = " << area << endl;

  return 0;
}

Output:-

Enter length of base and height: 22 5.9
Area = 64.9

Area of a triangle having base and height is given by, Area = 1/2 * (base * height)

Three variables base, height, and area of float data types are defined to store the values.

Area of a Triangle having Three Sides

Area of a triangle having three sides a, b and c is given by,
s = ( a + b + c )
area = ( s * (s-a) * (s-b) * (s-c) )
1/2

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
  float a, b, c, s, area;

  cout << "Enter length of three sides of triangle: ";
  cin >> a >> b >> c;

  s = (a + b + c) / 2;
  area= sqrt( s * (s-a) * (s-b) * (s-c) );

  cout << "Area = " << area << endl;

  return 0;
}

Output:-

Enter length of three sides of triangle: 4.5 8.9 12
Area = 16.6437

To, find area we need sqrt() function to calculate square root value. sqrt() function is defined under <math.h>.

C++ Program to Calculate the Area of Rectangle

The area of a rectangle is calculated by length * area.

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
  float length, width, area;

  cout << "Enter length and width of triangle: ";
  cin >> length >> width ;

  area= length* width;
  cout << "Area = " << area << endl;

  return 0;
}

Output:-

Enter length and width of triangle: 12.5 15.2
Area = 190

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Learn More C++ Programming Examples,

Leave a Reply