Showing posts with label Multiplication table using two dimensional array. Show all posts
Showing posts with label Multiplication table using two dimensional array. Show all posts

Multiplication table using two dimensional array

Program 29) Write a program to compute and print a multiplication table for numbers 1 to 5 as shown below:
                1     2     3     4     5
         1     1     2     3     4     5
         2     2     4     6     8    10
         3     3     6     .      .      .
         4     4     8     .      .      .
         5     5    10    .      .     25

This program uses a two dimensional array to store the table values. Each value is calculated using the control variables of the nested loops as follows:
                       product [i] [j] = row * column
where i denotes rows and j denotes columns of the product table. Since the indices i and j range from 0 to 4, we have introduced the following transformation:
                         row     = j + 1
                      column = J + 1 

                           PROGRAM

 #define ROWS 5
 #define COLUMNS 5
 main ( )
  {
         int row, column, product [ROWS] [COLUMNS] ;
         int i, j ;
         printf ("     MULTIPLICATION TABLE\n\n") ;
         printf ("  ") ;
         for (j = 1; j <= COLUMNS; J++)
             printf ("\n") ;
             printf ("____________________\n") ;
             for (i = 0; i< ROWS; i++)
             {
                  row = i + 1 ;
                  printf ("%2d |", row) ;
                  for (j = 1; j <= COLUMNS; J++)
                  {
                     column j ;
                     product [i] [j] = row * column ;
                     printf ("%4d", product [i] [j]) ;
                 }
                 printf ("\n") ;
             }
  }


Output:      MULTIPLICATION TABLE
                 1     2     3     4     5
           1    2     4     6     8    10
           2    3     6     9    12   15
           3    4     8    12   16   20
           4    5    10   15   20   25