Table program in C



Displaying a table in C programming language is more or less similar to that of counting. We use only one iteration and increment it with the value of which table is being printed.

Algorithm

Let's first see what should be the step-by-step procedure to print a table −

START
   Step 1 → Define table value n
   Step 2 → Iterate from i = n to (n*10)
   Step 3 → Display i
   Step 4 → Increment i by n
STOP

Pseudocode

Let's now see the pseudocode of this algorithm −

procedure table()

   Define table value n
   FOR value = n to (n*10) DO
      DISPLAY n
      Increment value by n
   END FOR

end procedure

Implementation

Now, we shall see the actual implementation of the program −

#include <stdio.h>

int main() {
   int i, j, n;

   n = 3;
   j = 1;
   
   for(i = n; i <= (n*10); i += n) {
      printf("%3d  x %2d  =  %3d\n", n, j, i);
      j++;
   }

   return 0;
}

Output

Output of this program should be −

  3  x  1  =    3
  3  x  2  =    6
  3  x  3  =    9
  3  x  4  =   12
  3  x  5  =   15
  3  x  6  =   18
  3  x  7  =   21
  3  x  8  =   24
  3  x  9  =   27
  3  x 10  =   30
loop_examples_in_c.htm
Advertisements