DEV Community

Rafid Safwan
Rafid Safwan

Posted on

My C projects today

I started learning C a few days ago. I learned a few basic things, variables, data types, a few operators and if statements.
Today, I completed two projects, a sphere calculator and an interest calculator.
My previous projects were madlibs game and a shopping cart program. I am hoping corrections if something could be done better

Here's my code for sphere calculator:

#include <stdio.h>
#include <math.h>

int main() {
float r = 0;
float A = 0;
float d = 0;
float v = 0;
const float PI = 3.1416;

printf("Enter radius: ");
scanf("%f", &r);

A = 4 * PI * pow(r, 2);
d = 2*r;
v = (4/3)*PI*pow(r, 3);

printf("The diameter of the circle is: %.4f\n", d);
printf("The area of the circle is: %.4f\n", A);
printf("The volume of the sphere is: %.4f\n", v);

return 0;

}
Enter fullscreen mode Exit fullscreen mode

This is the interest calculator:

#include <stdio.h>
#include <math.h>

int main() {

float principal = 0;
float rate = 0;
int years = 0;
int timescompounded = 0;
double total = 0;

printf("Enter principal: ");
scanf("%f", &principal);
printf("Enter rate: ");
scanf("%f", &rate);
printf("Enter years: ");
scanf("%d", &years);
printf("Enter timescompounded: ");
scanf("%d", &timescompounded);

rate = rate /100;

total = principal * pow(( 1 + rate / timescompounded), years*timescompounded);

printf("After %d years, the total amount of money will be: %.2f$\n", years, total);


    return 0;

}
Enter fullscreen mode Exit fullscreen mode

Let me know your suggestions on them.

Top comments (0)