Here's a program to print multiplication of first n numbers in C programming language with proper explanation. Here n is a natural number describing the number up to which tables need to be printed.

# include <stdio.h> # include <conio.h> void main() { int r, c, y, n ; clrscr() ; r = 1 ; printf("Enter a number up to which you want tables: "); scanf("%d", &n); printf("\n\nThe multiplication table for first %d numbers is :\n\n", n) ; do { c = 1 ; do { y = r * c ; printf("%5d", y) ; c = c + 1 ; } while(c <= 10) ; printf("\n\n") ; r = r + 1 ; } while(r <= n) ; getch() ; }

**Output of above program is**

Enter a number up to which you want tables: 5

The multiplication table for first 5 numbers is :

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

**Explanation of above program**

Just like any other C program, first we include all the necessary header files and defines a main() function. Now in this example, we're displaying a multiplication table of first n numbers and we are displaying it in a grid form (like a matrix see the output). In order to display the output like that we have to use two loops also called as

**nested loops**or**loop within loop**. So here we are using two do-while loops (any other loop will work fine).
Here we declared four integer variables - c, r, n and y. First lets understand what is the need of each variable.

- c - Its value ranges from 1 - 10. It is used as one operand to calculate the multiplication. Its value resets to 1 after a multiplication table of a particular number is calculated.
- n - It is a number entered by the user showing up to what number user wants the table calculation. If user enters 5 the table up to first 5 number will be displayed.
- r - It is the other operand in the calculation of multiplication. Its value ranges from 1 to n. It is incremented after a multiplication table of a particular number is displayed.
- y - It is a temporary variable and is used to hold each multiplication value and then that value is displayed.

Getting back to the explanation, first value of r is set to 1 (for calculation of 1's table). Then user is prompt to enter a number up to which tables are displayed. Then comes the nested do while loops. Inside first do while loop, we first set value of c to 1 and then we write the second do while loop. In the inner do while loop, we calculate the multiplication and stores it in y with this expression

**y = r * c;**

Next we print the value of y and increment c by 1. Now we check if c <= 10 and if it is then we continue inside the inner do while loop. But when c > 10 we come out of the inner loop and increment r by 1 (to start working on next number) and checks if r <= n. When r > n we terminate both the loops and hence terminate the execution of our program.

**What does %5d mean?**

If you notice inside printf() (line 17), you'll see %5d, now what does it mean? It means that you want an integer to be printed (%d) but with a padding of 5 (%5d). In other words, every number will be printed in a space of a 5 digit long number. If you look at the output you'll see that there's a space between every number. To see for yourself try to change the value to some other integer and based on that space between integers will increase or decrease.

Perfect! Thanks a lot, well explained

Glad to help you.

Nice work

It really helped me a lot thanks :)

Glad to help you. Keep following us for more such posts. :)

Thankss awsummm explanation

Glad to help you. Keep following us for more posts... :)

mast hai