-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.c
More file actions
56 lines (46 loc) · 1.48 KB
/
loop.c
File metadata and controls
56 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#define PI 3.14159f
float calc_area(int radius);
float calc_volume(int radius);
/*A simple function that reads in a radius and prints out the area and volume of a sphere ten times*/
int main(void){
int radius = 0;
char option;
int i;
for (i = 0; i < 10; i = i+1)
{
printf("Enter a value for the radius: ");
scanf("%d", &radius);
if(radius >= 0){
printf("Please select a command \n s - for surface \n v - for volume \n b - for both \n");
printf("Your command: ");
scanf(" %c", &option);
switch(option){
case 's': printf("For a radius of %i the area of the sphere is %.2f \n", radius, calc_area(radius));
break;
case 'v': printf("For a radius of %i the volume of the sphere is %.2f \n", radius, calc_volume(radius));
break;
case 'b': printf("For a radius of %i the area of the sphere is %.2f and its volume is %.2f \n", radius, calc_area(radius), calc_volume(radius) );
break;
default: printf("Please enter a valid command \n");
break;
}
}
else{
printf("You cannot enter a negative number \n");
}
}
return 0;
}
/* A function which calculates the surface area of a sphere */
float calc_area(int radius){
float sArea;
sArea = 4*PI*(radius*radius);
return sArea;
}
/* A function which calculates the volume of a sphere */
float calc_volume(int radius){
float sVolume;
sVolume = (4.000f/3.000f)*PI*(radius*radius*radius);
return sVolume;
}