Counting table program in C



Displaying a counting table involves nested iterations. The outer iteration (loop) will control rows and inner iteration will control rows.

Algorithm

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

START
   Step 1 → Set outer loop i from 1 to 10
   Step 2 → Set inner loop j from i to 100
   Step 3 → Display value of j
   Step 4 → Increment j by 10
   Step 5 → Close inner loop j
   Step 6 → Display newline character
   Step 7 → Close outer loop i
STOP

Pseudocode

Let's now see the pseudocode of this algorithm −

procedure counting_table(A, B)

   FOR i from 1 to 10 DO
      FOR j from i to 100 DO
         DISPLAY j
         j = j + 10
      END FOR
   DISPLAY NEWLINE
  END FOR

end procedure

Implementation

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

#include <stdio.h>

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

   for(i = 1; i <= 10; i++) {
      for(j = i; j <= 100; j += 10 )
         printf(" %3d", j);

      printf("\n");
   }

   return 0;
}

Output

Output of this program should be −

   1  11  21  31  41  51  61  71  81  91
   2  12  22  32  42  52  62  72  82  92
   3  13  23  33  43  53  63  73  83  93
   4  14  24  34  44  54  64  74  84  94
   5  15  25  35  45  55  65  75  85  95
   6  16  26  36  46  56  66  76  86  96
   7  17  27  37  47  57  67  77  87  97
   8  18  28  38  48  58  68  78  88  98
   9  19  29  39  49  59  69  79  89  99
  10  20  30  40  50  60  70  80  90 100
loop_examples_in_c.htm
Advertisements