Sunday 15 June 2014

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

Here are C++ program to find the area of triangle, rectangle,square and circle. First program is to finds the area of triangle given three sides. Second program is to finds the area and circumference of a circle, fourth and fifth program is to finds the area & perimeter of a rectangle and square. Fifth and sixth program is to calculate area of triangle, rectangle,circle using function overloading and class.


C++ program to find the area of triangle given three sides

#include<math.h>
#include<iostream>
using namespace std;
int main()
{
    float p,a,b,c,result;
    cout<<"Enter First side of triangle ";
    cin>>a;
    cout<<"Enter Second side of triangle ";
    cin>>b;
    cout<<"Enter Third side of triangle ";
    cin>>c;
    p=(a+b+c)/2;
    result=sqrt(p*(p-a)*(p-b)*(p-c));
    cout<<"Area of triangle "<<result<<endl;
    return 0;
}

OUTPUT




C++ program to find the area and circumference of a circle

#include<iostream>
using namespace std;
int main()
{
    float area,radius,circum;
    cout<<"Enter the radius of circle ";
    cin>>radius;
    area=22*radius*radius/7.0;
    circum=2*22*radius/7.0;
    cout<<"Area of circle "<<area<<endl;
    cout<<"Circumference of circle "<<circum;
    return 0;
}

OUTPUT




C++ program to find the area & perimeter square

#include<iostream>
using namespace std;
int main()
{
    int side,area,perimeter;
    cout<<"Enter side of square ";
    cin>>side;
    area=side*side;
    perimeter=4*side;
    cout<<"Area of square = "<<area<<endl;
    cout<<"Perimeter of square = "<<perimeter;
    return 0;
}

OUTPUT




C++ program to find the area & perimeter of a rectangle

#include<iostream>
using namespace std;
int main()
{
    int len,bre,area,perimeter;
    cout<<"Enter length and breadth of rectangle ";
    cin>>len>>bre;
    area=len*bre;
    perimeter=2*(len+bre);
    cout<<"Area of rectangle = "<<area<<endl;
    cout<<"Perimeter of rectangle = "<<perimeter;
    return 0;
}

OUTPUT




C++ program to calculate area of triangle, rectangle,square using function overloading

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
float area(float a)
{
   return a*a;
}
float area(float l,float b)
{
   return l*b;
}
float area(float a,float b,float c)
{
    float s=(a+b+c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}
int main()
{
    float a,b,c;
    int ch;
    cout<<"1.) Area of Square\n2.) Area of Rectangle\n3.) Area of Triangle\nEnter your choice : ";
    cin>>ch;
    switch(ch)
    {
    case 1:
        cout<<"Enter side of square : ";
        cin>>a;
        cout<<"Area of square : "<<area(a);
        break;
    case 2:
        cout<<"Enter length and breadth of rectangle : ";
        cin>>a>>b;
        cout<<"Area of rectangle : "<<area(a,b);
        break;
    case 3:
        cout<<"Enter three sides of triangle : ";
        cin>>a>>b>>c;
        cout<<"Area of triangle : "<<area(a,b,c);
        break;
    default:
        cout<<"Sorry wrong choice";
    }
    return 0;
}

OUTPUT







C++ program to calculate area of triangle, rectangle,circle and square using class

#include<iostream>
#include<math.h>
using namespace std;
class triangle
{
private:
    float s1,s2,s3;
    float area;
public:
    void gettri()
    {
       cout<<"Enter three sides of triangle : ";
       cin>>s1>>s2>>s3;
    }
    void areatri()
    {
        float s=(s1+s2+s3)/2;
        area=sqrt(s*(s-s1)*(s-s2)*(s-s3));
        cout<<"Area of triangle : "<<area;
    }
};
class rectangle
{
private:
    float lenght,breadth,area;
public:
    void getrect()
    {
       cout<<"Enter length and breadth of rectangle : ";
       cin>>lenght>>breadth;
    }
    float arearect()
    {
        area=lenght*breadth;
        cout<<"Area of rectangle : "<<area;
    }
};
class square
{
private:
    float side,area;
public:
    void getsqr()
    {
       cout<<"Enter side of square : ";
       cin>>side;
    }
    void areasqr()
    {
        area=side*side;
        cout<<"Area of Square : "<<area;
    }
};
class circle
{
private:
    float radius,area;
public:
    void getcir()
    {
       cout<<"Enter radius of circle : ";
       cin>>radius;
    }
    float areacir()
    {
        area=3.14*radius*radius;
        cout<<"Area of Circle : "<<area;
    }
};
int main()
{
    int ch;
    cout<<"1.) Area of Square\n2.) Area of Rectangle\n3.) Area of Triangle\n4.) Area of Circle\nEnter your choice : ";
    cin>>ch;
    switch(ch)
    {
    case 1:
        square ar1;
        ar1.getsqr();
        ar1.areasqr();
        break;
    case 2:
        rectangle ar2;
        ar2.getrect();
        ar2.arearect();
        break;
    case 3:
        triangle ar3;
        ar3.gettri();
        ar3.areatri();
        break;
    case 4:
        circle ar4;
        ar4.getcir();
        ar4.areacir();
        break;
    default:
        cout<<"Sorry wrong choice";
    }
    return 0;
}


OUTPUT





No comments:

Post a Comment