C++ | Area of different shapes

Introduction

This post will Teach you to create a program in C++ to Calculate the area of different shapes.The first one will calculate the area of Square. The next will calculate area of Circle. The next one will calculate area of Rectangle. And the last one will calculate the area of Equilateral Triangle. So you can create more program for more different shapes. Go enjoy the program. Lets begin…………………..

Program to calculate area of square

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,area;
//Input the side and save it in 'a'
cout<<"Enter the side of square"<<endl;
cin>>a;
//calculate area and save it in 'area'
area=a*a;
//show the output 'area'
cout<<"Area of square is "<<area;
//get character
getch();
}

Output

Enter the side of square
5
Area of square is 25

Program to calculate area of circle

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,area;
//Input the radius and save it in 'a'
cout<<"Enter the radius of circle"<<endl;
cin>>a;
//calculate area and save it in 'area'
area=3.14*a*a;
//show the output 'area'
cout<<"Area of circle is "<<area;
//get character
getch();
}

Output

Enter the radius of circle
2
Area of circle is 12.56

Program to calculate Area of Rectangle

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float l,b,area;
//Input the length and breadth and save it in 'l' and 'b' respectively
cout<<"Enter the length of rectangle"<<endl;
cin>>l;
cout<<"Enter the breadth of rectangle"<<endl;
cin>>b;
//calculate area and save it in 'area'
area=l*b;
//show the output 'area'
cout<<"Area of rectangle is "<<area;
//get character
getch();
}

Output

Enter the length of rectangle
2
Enter the breadth of rectangle
4
Area of rectangle is 8

Program to calculate area of Equilateral Triangle

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b,area;
//Input the side and save it in 'a'
cout<<"Enter the side of Equilateral Triangle"<<endl;
cin>>a;
cout<<"Enter the height of Equilateral Triangle"<<endl;
cin>>b;
//calculate area and save it in 'area'
area=(a*b)/2;
//show the output 'area'
cout<<"Area of  Equilateral Triangle is "<<area;
//get character
getch();
}

Output

Enter the side of Equilateral Triangle
2
Enter the height of Equilateral Triangle
5
Area of Equilateral Triangle  is 5

How does it work

  1. You enter the necessary inputs for calculation in and is saved in variables.
  2. The Area is calculated according to area formula and is saved in ‘area’.
  3. Variable ‘area’ is shown by cout.

Extending it

The program can be extended by using more different shapes of geometry. You can take the necessary inputs and calculate the area of the given geometry.

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as float (name the as you want.)
  5. Add cout and cin of variables of necessary inputs.(as shown in program)
  6. Calculate the area of  the variables and save them in ‘area’ variable.
  7. Cout the ‘area’ variable.

At the end

You learnt creating the c++ program of Area of geometrical shapes. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s