4. Program that contains an if statement that may be used to compute the area of a square or a triangle after prompting the user to type the first character of the figure names.

Chapter 5: 

Conditional Structures

Programming Exercise

Problem # 4:

Write a program that contains an if statement that may be used to compute the area of a square (area = side * side) or a triangle (area = 1/2 * base * height) after prompting the user to type the first character of the figure names (S or T).

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
    char c;
    float area, side, base, height;
    cout<<"Enter the first chracter of figure name:\nEnter S for square\nEnter T for triangle.";
    cin>>c;
    if(c=='S'||c=='s')
    {
        side=46;
        area=side*side;
        cout<<endl<<"Area of square is:"<<area;
    }
    else if(c=='T'||c=='t')
    {
        base=69;
        height=58;
        area=0.5*base*height;
        cout<<endl<<"Area of triangle is:"<<area;
    }
    else
    cout<<endl<<"Invalid input";
    return 0;
}


Let me know in the comment section if you have any question.

Previous Post:
3. Program to get three numbers from user for integer variables a, b and c. If a is not zero, find out whether it is the common divisor of b and c.

Comments