Answer to Question #266729 in C for Gladys

Question #266729

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.

1
Expert's answer
2021-11-16T05:21:50-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS