C++ | Area of different shapes using function

Note:- If you are looking for simple program of area of shapes(not using function), please see this.

C++ | Area of different shapes

else continue here……

Introduction

This is a tutorial of area of different shapes using function. The simple program(not using function) link is given above. The Program using function is given below. The program is extendable. Go enjoy the program. Lets begin…..

Program to find area of different shapes using function

#include<iostream.h>
#include<conio.h>
//function prototype
float cir(float);
float sq(float);
float rec(float,float);
//main function
void main()
{
//clear the screen.
clrscr();
//declare variables as int.
float a,r,l,b;
//Input the radius, sides and length and breadth.
cout<<“Enter the radius of circle”<<endl;
cin>>r;
cout<<“Enter the side of square”<<endl;
cin>>a;
cout<<“Enter the length and breadth of rectangle”<<endl;
cin>>l>>b;
//calculate area using functions and print it.
cout<<“Area of circle is “<<cir(r)<<endl;
cout<<“Area of square is “<<sq(a)<<endl;
cout<<“Area of rectangle is “<<rec(l,b)<<endl;
//get character
getch();
}

//functions
float cir(float x)
{
float area;
area=3.14*x*x;
return(area);
}

float sq(float x)
{
float area;
area=x*x;
return(area);
}

float rec(float x,float y)
{
float area;
area=x*y;
return(area);
}

Output

Enter the radius of circle
1
Enter the side of square
5
Enter the length and breadth of rectangle
4
5
Area of circle is 3.14
Area of square is 25
Area of rectangle is 20

How does it work

  1. You enter the necessary inputs.
  2. The function calculates the area.
  3. The area is printed.

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 and add there respective functions at end. Even you can do the same program to find the perimeter of different shapes.

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Declare the function prototype.
  3. Add void main.
  4. Start program by first clearing the screen.
  5. Declare the variables as int (name them as you want.)
  6. Add the cout and cin of necessary shapes.
  7. Add the cout of area and call the function.
  8. Add the function definition outside the void main().

At the end

You learnt creating the C++ program of Area of different shapes using function. So now enjoy the program.

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

Advertisements

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