Write a simple Menu-driven program that allows users to select from 1 to 4 to solve the area of
the following geometric shapes (Square, circle, rectangle and triangle). If the users enter numbers
not listed in the menu and “Invalid Input” will be displayed on the screen.
Search the formulas in finding the area of the different geometric shapes.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int main(){
const double PI = 3.1416;
double radius,side,side1,side2,base,height;
double area;
int ch=-1;
while(ch!=5){
printf("1. Calculate the area of square\n");
printf("2. Calculate the area of circle\n");
printf("3. Calculate the area of rectangle\n");
printf("4. Calculate the area of triangle\n");
printf("5. Exit\n");
scanf("%d",&ch);
switch(ch){
case 1:
{
printf("Enter a side of a square: ");
scanf("%lf",&side);
area = side * side;
printf("The area of a square: %.2f\n",area);
}
break;
case 2:
{
printf("Enter a radius of a circle: ");
scanf("%lf",&radius);
area = PI * radius * radius;
printf("The area of a circle: %.2f\n",area);
}
break;
case 3:
{
printf("Enter a side 1 of a rectangle: ");
scanf("%lf",&side1);
printf("Enter a side 2 of a rectangle: ");
scanf("%lf",&side2);
area = side1 * side2;
printf("The area of a rectangle: %.2f\n",area);
}
break;
case 4:
{
printf("Enter a base of a triangle: ");
scanf("%lf",&base);
printf("Enter a height of a triangle: ");
scanf("%lf",&height);
area = 0.5*base * height;
printf("The area of a rectangle: %.2f\n",area);
}
break;
case 5:
//exit
break;
default:
printf("Invalid Input\n");
break;
}
printf("\n\n");
}
return 0;
}
Comments
Leave a comment