Code:
/*Filename: Richacm2.c
Program: Calculate Area
*/
/*
---------------------------------------------------------
Calculate the area for a rectangle, triangle, circle,
ellipse, and sphere.
Use scanf() to retrieve values.
Calculate the area.
Use printf() to display.
--------------------------------------------------------
*/
#include <stdio.h>
#include <math.h>
#define PI 3.141593
int main()
{
/* Declare and Initialize All Variables*/
float area_rectangle, base_rect, height_rect;
float area_triangle, height_tri, base_tri;
float area_circle, radius_circ;
float area_ellipse, length_a_ellipse, length_b_ellipse;
float area_sphere, radius_sphere;
int option;
do
{
printf("Please type 1, 2, 3, 4, 5, or 0\n"
"1. Rectangle\n"
"2. Triangle\n"
"3. Circle\n"
"4. Ellipse\n"
"5. Sphere\n"
"0. Quit\n");
scanf("%d", &option);
if(option==1)
{
/*
---------------------------------------------------------
Area of Rectangle
-------------------------------------------------------
*/
/* Obtain Dimensions*/
printf("Enter base of rectangle: ");
scanf("%f",& base_rect);
printf("Enter height of rectangle: ");
scanf("%f",& height_rect);
/*Compute the Area*/
area_rectangle = base_rect * height_rect;
/*Output the results*/
printf("Base =%5.2f\nHeight =%5.2f\nArea of the Rectangle = %5.2f\n",
base_rect, height_rect, area_rectangle);
}
else if (option==2)
{
/*
---------------------------------------------------------
Area of Triangle
-------------------------------------------------------
*/
/* Obtain Dimensions*/
printf("Enter base of the triangle: ");
scanf("%f",& base_tri);
printf("Enter height of the triangle: ");
scanf("%f",& height_tri);
/*Compute the Area*/
area_triangle = (base_tri * height_tri)/2;
/*Output the results*/
printf("Base =%5.2f\nHeight =%5.2f\nArea of the Triangle = %5.2f\n",
base_tri, height_tri, area_triangle);
}
else if (option==3)
{
/*
---------------------------------------------------------
Area of Circle
-------------------------------------------------------
*/
/* Obtain Dimensions*/
printf("Enter radius of the circle: ");
scanf("%f",& radius_circ);
/*Compute the Area*/
area_circle = pow(radius_circ,2) * PI;
/*Output the results*/
printf("Radius =%5.2f\nArea of the Circle =%5.2f\n",
radius_circ, area_circle);
}
else if (option==4)
{
/*
---------------------------------------------------------
Area of Ellipse
-------------------------------------------------------
*/
/* Obtain Dimensions*/
printf("Enter length A of the ellipse: ");
scanf("%f",& length_a_ellipse);
printf("Enter length B of the ellipse: ");
scanf("%f",& length_b_ellipse);
/*Compute the Area*/
area_ellipse = PI * length_a_ellipse * length_b_ellipse;
/*Output the results*/
printf("Length A =%5.2f\nLength B =%5.2f\nArea of the Ellipse =%5.2f\n",
length_a_ellipse, length_b_ellipse, area_ellipse);
}
else if (option==5)
{
/*
---------------------------------------------------------
Surface Area of a Sphere
-------------------------------------------------------
*/
/* Obtain Dimensions*/
printf("Enter radius of the sphere: ");
scanf("%f",& radius_sphere);
/* Compute the Area */
area_sphere = pow(radius_sphere,2) * 4 * PI;
/* Output the Results */
printf("Radius =%5.2f\nSurface Area of the Sphere =%5.2f\n",
radius_sphere, area_sphere);
}
else
{
printf("Loop complete\n");
}
printf("Please type 1, 2, 3, 4, 5, or 0\n"
"1. Rectangle\n"
"2. Triangle\n"
"3. Circle\n"
"4. Ellipse\n"
"5. Sphere\n"
"0. Quit\n");
scanf("%d", &option);
}while(option);
do
scanf("%d", &option);
while(option>0);
return 0;
}
Thanks.