C++ | Perimeter of different shapes

Introduction

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

Program to calculate perimeter of square

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

Output

Enter the side of square
5
Perimeter of square is 20

Program to calculate perimeter of circle

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

Output

Enter the radius of circle
2
Perimeter of circle is 12.56

Program to calculate perimeter of Rectangle

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float l,b,peri;
//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 perimeter and save it in 'peri'
peri=2*(l+b);
//show the output 'peri'
cout<<"Perimeter of rectangle is "<<peri;
//get character
getch();
}

Output

Enter the length of rectangle
2
Enter the breadth of rectangle
1
Perimeter of rectangle is 6

Program to calculate perimeter of Equilateral Triangle

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

Output

Enter the side of Equilateral Triangle
2
Perimeter of Equilateral Triangle  is 6

How does it work

  1. You enter the necessary inputs for calculation in and is saved in variables.
  2. The Perimeter is calculated according to perimeter formula and is saved in ‘peri’.
  3. Variable ‘peri’ 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 perimeter 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 peri of  the variables and save them in ‘peri’ variable.
  7. Cout the ‘peri’ variable.

At the end

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

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