PRACTICE

Now that you've learned about the basics of writing C programs, let's practice that with a few exercises:

Practice program 1.

Write a program that calculates the volume of a sphere. Ask the user to enter the radius. The formula to calculate the volume of a sphere is:

𝒱 = 4/3 π 𝓇³

Here's one solution. Because we need to use π in our calculation, and 3.14159… is definitely a floating point value, I used float variables:

/* Calculate the volume of a sphere */

#include <stdio.h>

/* definitions */

#define PI 3.14159

int
main()
{
   /* let's use floating point numbers for this */

   float radius;
   float volume;

   /* read the radius from the user */

   puts("Calculate the volume of a sphere");
   puts("Enter the radius:");

   scanf("%f", &radius);

   /* calculate the volume, and print it */

   /* volume is 4/3 pi r^3 */

   volume = 4.0 * PI * radius * radius * radius / 3.0;
   printf("The volume is %f\n", volume);

   /* done */

   return 0;
}

Sample output from this program:

Calculate the volume of a sphere
Enter the radius:
1
The volume is 4.188787

By the way, since 4/3 π is just a number, you could also replace your volume calculation with the value of 4/3 π. I ran that in a calculator and got 4.188790205. It would be a good idea to mention that separate calculation in a comment, so you can remember it the next time you need to update your program. For example:

/* Calculate the volume of a sphere */

#include <stdio.h>

int
main()
{
   /* let's use floating point numbers for this */

   float radius;
   float volume;

   /* read the radius from the user */

   puts("Calculate the volume of a sphere");
   puts("Enter the radius:");

   scanf("%f", &radius);

   /* calculate the volume, and print it */

   /* volume is 4/3 pi r^3 */
   /* 4/3 pi is 4.188790205 */

   volume = radius * radius * radius * 4.188790205;
   printf("The volume is %f\n", volume);

   /* done */

   return 0;
}

Since I did that 4/3 π calculation separately, I didn't need the #define PI definition at the beginning of the program.

Practice program 2.

Write a program that converts Fahrenheit to Celsius. Ask the user to enter the temperature in Fahrenheit. The formula to convert Fahrenheit to Celsius is:

𝒞 = (ℱ − 32) × 5/9

Here's one solution. I used int variables here. Note that when performing calculations with integer values, you need to be careful about division. Normally, 5/9 would give 0.555… But here, both 5 and 9 are ints. When you divide two ints, you get an int. And 0.555… would be rounded to zero.

If you save the division to the end, you'll be okay. If you do the division up front, you'll get zero, because 5/9 is zero.

/* Convert Fahrenheit to Celsius */

#include <stdio.h>

int
main()
{
   /* let's do this one using integer values */

   int fahr;
   int celcius;

   /* read the Fahrenheit value */

   puts("Convert Fahrenheit to Celsius");
   puts("Enter the temperature in F");

   scanf("%d", &fahr);

   /* convert to Celcius and print the result */

   celcius = (fahr - 32) * 5 / 9;
   printf("that's %d Celcius\n", celcius);

   /* note that I saved the division to the end. that's because I'm
      using integer values, and 5/9 would give a value between 0 and 1
      (and as an integer, would be rounded to zero). Since C calculates
      from left to right, parentheses first, then * and /, then + and
      -, the division would happen first, and you'd have zero times
      (fahr-32). Let's retry that calculation with the division up
      front, so you can see what happens: */

   celcius = 5 / 9 * (fahr - 32);
   printf("with division first, that's %d Celcius\n", celcius);

   return 0;
}

Sample output from this program:

Convert Fahrenheit to Celsius
Enter the temperature in F
100
that's 37 Celcius
with division first, that's 0 Celcius

And since 5/9 is just a number equal to 0.555… you could use 0.555556 at the end of your Celcius calculation, rather than the separate multiplication and division. C will recognize that you are multiplying an integer value (fahr − 32) with a floating point value 0.555556, and store it as an int when it saves the result to celcius. But I recommend avoiding mixing different variable types in your calculations when possible, so I did the separate multiplication and division here.

   celcius = (fahr - 32) * 0.555556;
   printf("that's %d Celcius\n", celcius);