PRACTICE

Now that you've learned about flow control in C programs, let's practice that with a few exercises:

Practice program 1.

Write a program that converts different values from Fahrenheit to Celsius. This is similar to the practice program in part 2, but in this case iterate from Fahrenheit temperatures from 98 to 104. The formula to convert Fahrenheit to Celcius is:

𝒞 = (ℱ − 32) × 5/9

You can solve this using different kinds of loops, but a for loop would be a great fit because the loop has define start and stop points. You could do this with int values, but you can do loops with float values as well, and that would allow for better precision. So let's use floating point temperatures:

#include <stdio.h>

int
main()
{
   float fahr;
   float celcius;

   /* print a conversation table from F to C (for 98F to 104F) */

   for (fahr = 98.0; fahr <= 104.0; fahr = fahr + 1.0) {
      celcius = (fahr - 32) * 5.0 / 9.0;

      printf("%f F -> %f C\n", fahr, celcius);
   }

   return 0;
}

Sample output from this program:

98.000000 F -> 36.666668 C
99.000000 F -> 37.222221 C
100.000000 F -> 37.777779 C
101.000000 F -> 38.333332 C
102.000000 F -> 38.888889 C
103.000000 F -> 39.444443 C
104.000000 F -> 40.000000 C

Practice program 2.

Write a program that calculates the factorial of a number. The factorial (𝓃!) of a non-negative integer 𝓃 is defined as the product (multiplication) of that number times all the numbers down to 1. The factorial of zero (0!) is 1, and the factorial of one (1!) is also 1. For example, the factorial of five (5!) is 5×4×3×2×1.

We'll learn a different way to calculate factorials in the next unit, when we learn about functions. But factorial is easy to calculate with what we've learned so far.

#include <stdio.h>

int
main()
{
   int num;
   int iter;
   int fact = 1;

   puts("Enter a non-negative integer:");
   scanf("%d", &num);

   if (num < 0) {
      puts("Oops! That needs to be 0 or greater");
   }
   else {
      for (iter = num; iter > 0; iter = iter - 1) {
         fact = fact * iter;
      }

      printf("%d! is %d\n", num, fact);
   }

   return 0;
}

Sample output:

Enter a non-negative integer:
3
3! is 6

Note that 0! is 1, because we initialized fact to 1, and the for loop doesn't happen if num is zero.

Another way to write this program is to initialize the answer to 1, and count up as you multiply the factorial.

Practice program 3.

Write a program that asks the user to enter a single letter. Continue looping until the user enters Q or q.

This calls for a loop that reads a single letter at each iteration, and exits only when the program detects the correct letter. Which loop will you try? If we used a while loop, the test would happen at the beginning—so we'd have to read a letter before entering the loop. But if we use a do-while loop, the test will happen at the end, after we've read the letter. So let's use a do-while loop here:

#include <stdio.h>

int
main()
{
   char ch;

   puts("Enter a letter (enter Q or q to quit)");

   do {
      scanf("%c", &ch);
      printf(" -> %c\n", ch);
   } while ((ch != 'q') && (ch != 'Q'));

   return 0;
}

Sample output:

Enter a letter (enter Q or q to quit)
asdf
 -> a
 -> s
 -> d
 -> f
 -> 

asqw
 -> a
 -> s
 -> q

You can see that scanf is really reading a line at a time, and passing each character back as the loop asks scanf for the next letter. This makes sense, because you had to press Enter after entering the characters.

In a later unit, we'll learn how to read letters directly from the keyboard.

Practice program 4.

Do you know the "folk" song about 99 bottles of beer? It's a song about beer on a shelf, presumably in a pub. If you take down one bottle of beer and pass it around, you have 1 fewer bottles on the wall. This is a very tedious song to sing, but easy for a program to iterate. Write a program to loop through the lyrics "99 bottles of beer on the wall. 99 bottles of beer! // Take one down, pass it around. 98 bottles of beer on the wall." Stop when you reach 0 bottles.

For this program, it's easy to use a for loop that counts backwards from 99 to 1. Each verse inthe song is identical except for the count, so we can put that in the loop, too.

#include <stdio.h>

int
main()
{
   int bottles;

   for (bottles = 99; bottles > 0; bottles = bottles - 1) {
      printf("%d bottles of beer on the wall. %d bottles of beer!\n", bottles,
             bottles);
      printf
         ("   Take one down, pass it around. %d bottles of beer on the wall.\n",
          bottles - 1);
   }

   return 0;
}

Note that when you reach 1 bottle of beer, the program still says "bottles." That's because we've simplified the program. One way to fix this is to have the loop count from 99 to 2, and have a separate pair of puts statements at the end to account for "1 bottle of beer on the wall." This can be a puts because you can hard-code the last verse to be exactly what you need. You can also have a different ending to the song using the same method.