Problem statement
Problem: Write a 'C' program to find Volumes of:
i) Sphere
ii) Cube
iii) Cone
You need to accept character from user for choice of figure.
Characters for figure are as 'S' for Sphere, 'C' for Cube and 'D' for Cone.
According to figure chosen, Accept radius/height/side for figures from user.
Compute the volume according to the given values and print the volume.
/* Including the header files */
#include<stdio.h>
#define PI 3.14159
/* The execution of the program starts here */
int main() {
char sh;
/* a. Read the choice of the shape from terminal */
printf("\nEnter S - Sphere; C - Cube; D - Cone: ");
scanf("%c", &sh);
/* S = Sphere */
if (sh == 'S' || sh == 's')
{
printf("\nShape selected: Sphere (S)");
printf("\nEnter the Radius: ");
float r = 0.0;
scanf("%f", &r);
float vol = (4.0 * PI * r*r*r)/ 3.0;
printf("\nThe Volume of Sphere: %f", vol);
}
else if (sh == 'C' || sh == 'c')
{ /* C = Cube */
printf("\nShape selected: Cube (C)");
printf("\nEnter the side length: ");
float l = 0.0;
scanf("%f", &l);
float vol = l*l*l;
printf("\nThe Volume of Sphere: %f", vol);
}
else if (sh == 'D' || sh == 'd')
{
/* D = Cone */
printf("\nShape selected: Cone (D)");
printf("\nEnter the Radius: ");
float r = 0.0;
scanf("%f", &r);
printf("\nEnter the Height: ");
float h = 0.0;
scanf("%f", &h);
float vol = (1.0 * PI * r*r*h)/ 3.0;
printf("\nThe Volume of Sphere: %f", vol);
}
else
{
printf("\nWrong Key! No SHAPE selected ... Exits ... !!");
}
printf("\n");
return 0;
}
/*
TEST CASES
--------------
$ ./a.out
Enter S - Sphere; C - Cube; D - Cone: s
Shape selected: Sphere (S)
Enter the Radius: 9.6
The Volume of Sphere: 3705.970703
------------------------------------
$ ./a.out
Enter S - Sphere; C - Cube; D - Cone: d
Shape selected: Cone (D)
Enter the Radius: 2.1
Enter the Height: 6
The Volume of Sphere: 27.708820
-------------------------------------
$ ./a.out
Enter S - Sphere; C - Cube; D - Cone: c
Shape selected: Cube (C)
Enter the side length: 2
The Volume of Sphere: 8.000000
*/
No comments:
Post a Comment