Thursday 9 February 2012

Find area of triangle rectangle and circle using C ++ programming

Program to find the area of triangle, rectangle, and circle
#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 explanation:

1."iostream.h" is input output stream header file is used to including the function like cin and cout.
2."conio.h" is console input output header file is used for including the function like 'getch()', 'clrscr()'.
3.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.
4.void main() is a main function in which whole body of program is defined.
5.'char' is data type which is used to define character variable, here 'ch' is a character variable.
6.'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.
7. radius is a float variable.
8. 'int' is data type which is used to dafine integer variable, hera 'choice1' has the integer type value.
9.clrscr() is a library function of "conio.h".
10. 'cout' function is used to show the value and symbol '<<' is used with 'cout' function.
11.'cin' function is used to take input, here if 'choice1' take input like (1,2,3).
12.'if' is to define the condition like 'choice1=1' then area of triangle will be calculated.
13.'cout' is used to print the three side of triangle.
14. 'cin' is used to scan the value n1, n2, n3.
11. Now function is calling area(n1, n2, n3) is stored in 'ar'.
12.print the content of ar.
13. 'choice1=2' condition define the area of rectangle.
14.enter the value of length.
15.The value of len(length) will be scan.
16. Enter the value of width.
17. The value of width will be scan.
18. The area of rectangle will be print.
19.'choice==3' is used for calculate of area of circle.
20.enter the radius.
21. Scan the value of radius.
22.Print the area of circle.

No comments:

Post a Comment