I've just wrote my first real C program. It calculates the dimensions of a circle or a sphere based on the user input.
The program asks the user which dimension they are entering and then prints the values of the remaining dimensions.
#include <stdio.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<stdlib.h>
double radius, surfaceArea, area, volume, diameter, circumference;
char inputType[20];
char inputValue[30];
void printvalues(double radius);
int main()
{
printf("Sphere and Circle Calculator\n");
printf("Are you entering radius, diameter, circumference, area, surface area or volume? ");
fgets(inputType, 20, stdin);
if (_stricmp(inputType, "radius\n") == 0 || _stricmp(inputType, "r\n") == 0)
{
printf("Please enter the radius: ");
fgets(inputValue, 20, stdin);
radius = atof(inputValue);
printvalues(radius);
}
else if (_stricmp(inputType, "diameter\n") == 0 || _stricmp(inputType, "d\n") == 0)
{
printf("Please enter the diameter: ");
fgets(inputValue, 20, stdin);
diameter = atof(inputValue);
radius = diameter / 2;
printvalues(radius);
}
else if (_stricmp(inputType, "circumference\n") == 0 || _stricmp(inputType, "c\n") == 0)
{
printf("Please enter the circumference: ");
fgets(inputValue, 20, stdin);
circumference = atof(inputValue);
radius = (circumference/M_PI)/M_PI;
printvalues(radius);
}
else if (_stricmp(inputType, "area\n") == 0 || _stricmp(inputType, "a\n") == 0)
{
printf("Please enter the 2D area: ");
fgets(inputValue, 20, stdin);
area = atof(inputValue);
radius = sqrt(area / M_PI);
printvalues(radius);
}
else if (_stricmp(inputType, "surface area\n") == 0 || _stricmp(inputType, "s\n") == 0 || _stricmp(inputType, "surface\n") == 0)
{
printf("Please enter the 3D surface area: ");
fgets(inputValue, 20, stdin);
surfaceArea = atof(inputValue); //r = √(A/(4π)) r=((V/π)(3/4))1/3
radius = sqrt(surfaceArea / (4*M_PI));
printvalues(radius);
}
else if (_stricmp(inputType, "volume\n") == 0 || _stricmp(inputType, "v\n") == 0 )
{
printf("Please enter the 3D surface area: ");
fgets(inputValue, 20, stdin);
volume = atof(inputValue); //r=((V/π)(3/4))1/3
radius = cbrt(volume/M_PI)*0.75;
printvalues(radius);
}
else if (_stricmp(inputType, "exit\n") == 0 || _stricmp(inputType, "q\n") == 0)
{
return 0;
}
else
{
printf("You did not enter a valid value\n");
}
}
void printvalues(double radius)
{
diameter = radius * 2;
circumference = diameter * M_PI;
area = M_PI * (radius * radius);
volume = (4.0 / 3.0) * M_PI * pow(radius, 3);
surfaceArea = 4.0 * M_PI * pow(radius, 2);
printf("The radius is: %f \n", radius);
printf("The diameter is: %f \n", diameter);
printf("The circumference is: %f \n", circumference);
printf("The 2D area is %f \n", area);
printf("The 3D volume is: %f \n", volume);
printf("The surface area is: %f \n", surfaceArea);
}
Everything seems to be working correctly but I'd like reviews so I can learn better C.
The next thing to do is make a loop so the user can make repeated calculations and to add code to let the user quit at anytime by typing exit.