Programming in C

(Barry) #1
Exercises 63

Now that you are familiar with all the basic looping constructs provided by the C
language, you are ready to learn about another class of language statements that enable
you to make decisions during the execution of a program.These decision-making capa-
bilities are described in detail in Chapter 6, “Making Decisions.” First, try the exercises
that follow to be certain you understand how to work with loops in C.


Exercises



  1. Type in and run the nine programs presented in this chapter. Compare the output
    produced by each program with the output presented after each program in the
    text.

  2. Write a program to generate and display a table of nand n^2 , for integer values of n
    ranging from 1 to 10. Be certain to print appropriate column headings.

  3. A triangular number can also be generated by the formula
    triangularNumber = n (n + 1) / 2


for any integer value of n.For example, the 10th triangular number, 55, can be
generated by substituting 10 as the value for nin the preceding formula.Write a
program that generates a table of triangular numbers using the preceding formula.
Have the program generate every fifth triangular number between 5 and 50 (that
is, 5, 10, 15, ..., 50).


  1. The factorial of an integer n,written n!, is the product of the consecutive integers
    1 through n.For example, 5 factorial is calculated as
    5! = 5 x 4 x 3 x 2 x 1 = 120


Write a program to generate and print a table of the first 10 factorials.


  1. The following perfectly valid C program was written without much attention paid
    to its format. As you will observe, the program is not very readable. (And believe it
    or not, it is even possible to make this program significantly more unreadable!)
    Using the programs presented in this chapter as examples, reformat the program so
    that it is more readable.Then type the program into the computer and run it.
    #include <stdio.h>
    int main(void){
    int n,two_to_the_n;
    printf("TABLE OF POWERS OF TWO\n\n");
    printf(" n 2 to the n\n");
    printf("--- ---------------\n");
    two_to_the_n=1;
    for(n=0;n<=10;++n){
    printf("%2i %i\n",n,two_to_the_n); two_to_the_n*=2;}
    return 0;}

Free download pdf