Sunday, 26 May 2013

Area of two triangles - CPP Program

#include<iostream.h>
#include<conio.h>
#include<math.h>
class right_triangle
{
double base;
double height;
   public:
void initialize(double,double);
double area();
double peri();
};
void right_triangle::initialize(double b, double h)
{
base=b;
height=h;
}
double right_triangle::area()
{
return (0.5*base*height);
}
double right_triangle::peri()
{
double hypt;
hypt = sqrt(base*base + height*height);
return base+height+hypt;
}
int main()
{
right_triangle r1,r2;
double bs,ht;
//Initializing triangles
cout<<"\nINPUT\n";
cout<<"\nEnter base of first triangle : ";
cin>>bs;
cout<<"Enter height of first triangle : ";
cin>>ht;
r1.initialize(bs,ht);
cout<<"\nEnter base of second triangle : ";
cin>>bs;
cout<<"Enter height of second triangle : ";
cin>>ht;
r2.initialize(bs,ht);
//Calculating area and perimeter
cout<<"\nArea of first triangle       : "<<r1.area();
cout<<"\nPerimeter of first triangle  : "<<r1.peri();
cout<<"\n\nArea of second triangle      : "<<r2.area();
cout<<"\nPerimeter of second triangle : "<<r2.peri();
return 0;
}


Output:
INPUT
Enter base of first triangle : 7
Enter height of first triangle : 4
Enter base of second triangle : 6
Enter height of second triangle : 2
Area of first triangle       : 14
Perimeter of first triangle  : 19.0623
Area of second triangle      : 6
Perimeter of second triangle : 14.3246
Process returned 0 (0x0)   execution time : 26.804 s
Press any key to continue.

No comments:

Post a Comment