Skip to main content

Section 4.8 Using Variables in Loops: Practice!

Activity 4.5.

Please print a multiplication table (for multiplication by 7) to the screen, in the following form:
        1 x 7 = 7
        2 x 7 = 14
        3 x 7 = 21
        4 x 7 = 28
        5 x 7 = 35
        6 x 7 = 42
        7 x 7 = 49
        8 x 7 = 56
        9 x 7 = 63
        10 x 7 = 70
      
Be sure to use a loop here, do not write 10 printf() statements...
As a reminder, here is the format of a for-loop that runs 10 times:
int i; /* a variable, for example by the name of i, is needed */
for (i=1; i<=10; i++) {
   /* code that needs to be repeated goes here */
}
Through this loop, the variable i successively gets assigned values from 1 to 10. In the first run through the loop i has the value 1, in the next run-through that value is increased to 2, then to 3, all the way up to and including 10.

Compile and run the code below in your own C environment.

#include <stdio.h>

int main(void) {
    /* Please write your code here */


    return(0);
}

Investigate 4.6.

How could you easily change the output of this program, so that instead of each line reading i x 7, where i is that incrementing variable, it prints as 7 x i? Or, what if you wanted to print a multiplication table for a different number, instead of 7? Try out some of these changes if you have time!