Write a program that displays the following menu: If the user enters 1, the prog
ID: 3535086 • Letter: W
Question
Write a program that displays the following menu:
If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the following formula:
area = PI * r^2
(Use 3.14159 for PI)
If the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle's area. Use the following formula:
area = length * width
If the user enters 3 the program should ask for the length of the triangle's base and its height, and then display its area. Use the following formula:
area = base * height * 0.5
If the user enters 4, the program should end.
Input Validation: Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. Do not accept negative values for the circle s radius, the rectangle s length or width, or the triangle s base or height.
Explanation / Answer
#include<stdio.h>
int main()
{
int a;
float rad,arr,len,wid,bas,hei;
scanf("%d",&a);
switch(a)
{
case 1:
printf("Enter the radius:-/n");
scanf("%f",&rad);
arr=3.14159*rad*rad
printf("The area is %f",arr);
break;
case 2:
printf("Enter the length and width:-/n");
scanf("%f%f",&len,&wid);
arr=len*wid;
printf("The area is %f",arr);
break;
case 3:
printf("Enter the base and height:-/n");
scanf("%f%f",&bas,&hei);
arr=bas * hei * 0.5
printf("The area is %f",arr);
break;
case 4:
printf("Program ends");
break;
default:
printf("Input not valid");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.