C Program to Print Multiplication Table

A multiplication table of numbers is created by multiplying a constant integer by a number of repetitions ranging from 1 to 10.

Example:
If the given input is 6, then the multiplication table is:

 6 x 1 = 6
 6 x 2 = 12
 6 x 3 = 18
 6 x 4 = 24
 6 x 5 = 30
 6 x 6 = 36
 6 x 7 = 42
 6 x 8 = 48
 6 x 9 = 54
 6 x 10 = 60
Problem Description

Write a C program that will print the multiplication table of a number.

Problem Solution

1. Take the number as input.
2. Store the number in a variable.
3. Using loops print the number and its product with numbers 1-10 as output.
4. Print the result.

Multiplication Table in C can be found out in following ways:

advertisement
advertisement
Method 1: (Using For Loop)

In this approach, we will use a for loop to calculate the multiplication table of a number.

Program/Source Code

Here is source code of the C Program to print Multiplication table using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to print Multiplication table using for loop
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int n, i;
  10.     printf("Enter a number: ");
  11.     scanf("%d", &n);
  12.     printf("Multiplication table of %d:\n ", n);
  13.     printf("--------------------------\n");
  14.     for (i = 1; i <= 10; i++)
  15.         printf("%d x %d = %d\n", n, i, n * i);
  16.     return 0;
  17. }
Program Explanation

1. Take a number as input and store it in the variable n.
2. Using for loop starting from 1 to 10, print the variable n, i and (n * i).
3. Prints the multiplication table of the number.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Time complexity: O(1)
The time complexity of the multiplication table is O(1).

Space Complexity: O(1)
The space complexity of this program is O(1), since we are not using any extra space.

Runtime Test Cases

In this case, we enter the value “6” as input to print the multiplication table.

Enter the Number: 6
Multiplication table of 6:
--------------------------
 6 x 1 = 6
 6 x 2 = 12
 6 x 3 = 18
 6 x 4 = 24
 6 x 5 = 30
 6 x 6 = 36
 6 x 7 = 42
 6 x 8 = 48
 6 x 9 = 54
 6 x 10 = 60

advertisement
Method 2: (Using While Loop)

In this approach, we will use a while loop to calculate the multiplication table of a number.

Example:

Input:
Enter the Number: 12

Output:

Multiplication table of 12:
--------------------------
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
Program/Source Code

Here is source code of the C Program to print Multiplication table using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
  1. /*
  2.  * C Program to print Multiplication table using while loop
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int number, i = 1;
  10.  
  11.     printf(" Enter the Number:");
  12.     scanf("%d", &number);
  13.     printf("Multiplication table of %d:\n ", number);
  14.     printf("--------------------------\n");
  15.     while (i <= 10)
  16.     {
  17.         printf(" %d x %d = %d \n ", number, i, number * i);
  18.         i++;
  19.     }
  20.     return 0;
  21. }
Program Explanation

1. Take a number as input and store it in the variable number.
2. Using while loop starting from 1 to 10, print the variable number, i and (number * i) as output and exit.

Runtime Test Cases
In this case, we enter the value "12" as input to print the multiplication table.
Enter the Number: 12
Multiplication table of 12:
--------------------------
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

Method 3: (Using Do While Loop)

In this approach, we will use a do-while loop to calculate the multiplication table of a number.

Program/Source Code

Here is source code of the C Program to print Multiplication table using do while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to print Multiplication table using do while loop
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int n, i = 1;
  10.     printf("Enter a number: ");
  11.     scanf("%d", &n);
  12.     do
  13.     {
  14.         printf("%d x %d = %d\n", n, i, n * i);
  15.         i++;
  16.     } while (i <= 10);
  17.     return 0;
  18. }
Program Explanation

1. Take a number as input and store it in the variable n.
2. Calculate the multiplication table using a do-while loop.
3. For any number, the do-while loop starts from 1 and ends at 10 and prints the multiplication table of the number.

Time complexity: O(1)
The time complexity of the multiplication table is O(1).

Space Complexity: O(1)
The space complexity of this program is O(1), since we are not using any extra space.

Runtime Test Cases

In this case, we enter the value “17” as input to print the multiplication table.

Enter a number: 17
 
17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170

Method 4: (Using Function)

This approach takes the input and inside a separate function, print the multiplication table of the number.

Methods used:
void multiplicationTable(int) – This function will print the multiplication table of the number.

Example:

Input:
Enter a number : 8
 
Output:
Multiplication Table for 8:
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
Program/Source Code

Here is source code of the C Program to print Multiplication table using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to print Multiplication table using function
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. void multiplicationTable(int num)
  8. {
  9.     printf("Multiplication Table for %d:\n", num);
  10.     for (int i = 1; i <= 10; i++)
  11.     {
  12.         // Print the table using a suitable format
  13.         printf("%d x %d = %d\n", num, i, num * i); 
  14.     }
  15. }
  16.  
  17. int main(void)
  18. {
  19.     int num;
  20.     /* Ask the user for a number and store it in a variable*/
  21.     puts("Enter a number : ");
  22.     scanf("%d", &num);
  23.     multiplicationTable(num);
  24. }
Program Explanation

1. Take a number as input and store it in the variable num.
2. The variable num is passed to the multiplicationTable function multiplicationTable(num).
3. The multiplicationTable function prints the multiplication table of the number using a for loop and then print in a suitable format.

Time complexity: O(n)
The time complexity of this program is O(n), where n is the number of rows in the table.

Space Complexity: O(1)
The space complexity of this program is O(1), since we are not using any extra space.

Runtime Test Cases

In this case, we enter the value “8” as input to print the multiplication table.

 
Enter a number : 
8
Multiplication Table for 8:
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80

Method 5: (Using Recursion)

In this approach, calculate the multiplication table of a number using recursion.

Program/Source Code

Here is source code of the C Program to print Multiplication table using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to print Multiplication table using recursion
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int multiplicationTable(int num, int i)
  8. {
  9.     if (i > 10)
  10.     {
  11.         return;
  12.     }
  13.     printf("%d x %d = %d\n", num, i, num * i);
  14.     multiplicationTable(num, i + 1);
  15. }
  16.  
  17. int main(void)
  18. {
  19.     int num;
  20.  
  21.     // Ask the user for a number and store it in a variable
  22.     puts("Enter a number : ");
  23.     scanf("%d", &num);
  24.     multiplicationTable(num, 1);
  25. }
Program Explanation

1. Take a number as input and store it in the variable num.
2. The variable num and the value 1 are passed to the multiplicationTable function multiplicationTable(num, 1);.
3. The multiplicationTable function is a recursive function which terminates when the value of i is greater than 10.
4. For every step, the function prints the entry for that i and then calls itself again with the value of i incremented by 1.
5. Prints the multiplication table of the number.

Time complexity: O(n)
The time complexity of this program is O(n), where n is the number of rows in the table.

Space Complexity: O(1)
The space complexity of this program is O(1), since we are not using any extra space.

Runtime Test Cases

In this case, we enter the value “23” as input to print the multiplication table.

Enter a number : 23
 
23 x 1 = 23
23 x 2 = 46
23 x 3 = 69
23 x 4 = 92
23 x 5 = 115
23 x 6 = 138
23 x 7 = 161
23 x 8 = 184
23 x 9 = 207
23 x 10 = 230

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.