Area of various shapes
Write a program in C, which is a Menu-Driven Program to compute the area of the various geometrical shape.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
float radius, length, width, base, height, side;
float area;
printf("1. Area of circle");
printf("\n2. Area of Rectangle");
printf("\n3. Area of Triangle");
printf("\n4. Area of Square");
printf("\nEnter your choice\t:");
scanf("%d",&ch);
if(ch == 1)
{
printf("Enter radius of the circle\t:");
scanf("%f",&radius);
area = 3.14 * radius * radius;
printf("\n area = %f",area);
}
else if(ch == 2)
{
printf("Enter the length and width of the rectangle\t:");
scanf("%f %f",&length,&width);
area = length * width;
printf("\n area = %f",area);
}
else if(ch == 3)
{
printf("Enter the base and height of the rectangle\t:");
scanf("%f %f",&base,&height);
area = 0.5 * base * height;
printf("\n area = %f",area);
}
else if(ch == 4)
{
printf("Enter side of the square\t:");
scanf("%f",&side);
area = side * side;
printf("\n area = %f",area);
}
else
{
printf("Incorrect input");
}
getch();
}
Output
********** Run1 **********
1. Area of circle
2. Area of Rectangle
3. Area of Triangle
4. Area of Square
Enter your choice :1
Enter radius of the circle :3
area = 28.260000
********** Run2 **********
1. Area of circle
2. Area of Rectangle
3. Area of Triangle
4. Area of Square
Enter your choice :3
Enter the base and height of the rectangle :3
5
area = 7.500000
Explanation
Geometrical shapes that are considered are circle, rectangle, triangle and square.
First of all, menu is created and user is asked for his/her choice. Based upon the choice entered, appropriate action is taken using if-else construct. For example, in the above program,
if user enters '1' as his choice then, area of circle is calculated.
if user enters '2' as his choice then, area of rectangle is calculated.
and so on.
First of all, menu is created and user is asked for his/her choice. Based upon the choice entered, appropriate action is taken using if-else construct. For example, in the above program,
if user enters '1' as his choice then, area of circle is calculated.
if user enters '2' as his choice then, area of rectangle is calculated.
and so on.
if(ch == 1)
{
//area of circle
}
else if(ch == 2)
{
//area of rectangle
}
else if(ch == 3)
{
//area of triangle
}
else if(ch == 4)
{
//area of square
}
else
{
printf("Incorrect input");
}