Thursday 16 February 2012

C++ program to find the area of rectangle, area of circle and area of triangle


C++ program to find the area of rectangle, area of circle and area of triangle
#include "iostream.h"
#include "conio.h"
#include "iomanip.h"


float area(float a, float b , float c);
float area(float l, float w);
float  area(float  r);
void main()
{
 char ch;
 float len, wid, n1, n2, n3, ar;
 float  radius;
 int choice1;
 clrscr();
 cout << "\n1. For area of triangle ";
 cout << "\n2. For area of rectangle ";
 cout << "\n3.  For area of circle ";
 cin >> choice1;
 if (choice1 == 1)
 {
  cout << "\nEnter the three sides of triangle : ";
  cin >> n1 >> n2 >> n3;
  ar = area(n1, n2, n3);
  cout << "\nArea of triangle is: " << ar;
 }
 if (choice1 == 2)
 {
  cout << "\nEnter the length ";
  cin >> len;
  cout << "\nEnter the width: ";
  cin >> wid;
  cout << "\nArea of rectangle is: " << area(len, wid);
 }
 if (choice1 == 3)
 {
  cout << "\nEnter the radius ";
  cin >> radius;
  cout << "\n Area of circle " << area(radius);
 }
}

step by step explanaition:

  • "iostream.h" is input output stream header file is used to including the function like cin and cout.
  • "conio.h" is console input output header file is used for including the function like 'getch()', 'clrscr()'.
  • function overloading is used on the function 'float area'. All float variable is used in 'float area' function like a,b,c,l,w,r have float value.
  • void main() is a main function in which whole body of program is defined.
  • 'char' is data type which is used to define character variable, here 'ch' is a character variable.
  • 'float' is a data type which is used to define float variable, here len(length), wid(width), n1, n2, n3, ar(area) are the float variable.
  • radius is a float variable.
  • 'int' is data type which is used to dafine integer variable, hera 'choice1' has the integer type value.
  • clrscr() is a library function of  "conio.h".
  • 'cout' function is used to show the value and  symbol '<<' is used with 'cout' function. 
  • 'cin' function is used to take input, here if 'choice1' take input like (1,2,3).
  • 'if' is to define the condition like 'choice1=1' then area of triangle will be calculated.
  • 'cout' is used to print the three side of triangle.
  • 'cin' is used to scan the value n1, n2, n3.
  • Now function is calling area(n1, n2, n3) is stored in 'ar'. 
  • print the content of ar.
  • 'choice1=2' condition define the area of rectangle.
  • enter the value of length.
  • The value of len(length) will be scan.
  • Enter the value of width.
  • The value of width will be scan.
  • The area of rectangle will be print.
  • 'choice==3' is used for calculate of area of circle.
  • enter the radius. 
  • Scan the value of radius.
  • Print the area of circle.

 To know more about programming in C and C++, just visit our step by step tutorial of this site. They will be surely helpful to you.

No comments:

Post a Comment