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
Write a C program that will print the multiplication table of a number.
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:
- Multiplication Table in C using For Loop
- Multiplication Table in C using While Loop
- Multiplication Table in C using Do While Loop
- Multiplication Table in C using Function
- Multiplication Table in C using Recursion
In this approach, we will use a for loop to calculate the multiplication table of a number.
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.
/*
* C Program to print Multiplication table using for loop
*/
#include <stdio.h>
int main()
{
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
printf("Multiplication table of %d:\n ", n);
printf("--------------------------\n");
for (i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n * i);
return 0;
}
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.
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.
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
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
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.
/*
* C Program to print Multiplication table using while loop
*/
#include <stdio.h>
int main()
{
int number, i = 1;
printf(" Enter the Number:");
scanf("%d", &number);
printf("Multiplication table of %d:\n ", number);
printf("--------------------------\n");
while (i <= 10)
{
printf(" %d x %d = %d \n ", number, i, number * i);
i++;
}
return 0;
}
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.
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
In this approach, we will use a do-while loop to calculate the multiplication table of a number.
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.
/*
* C Program to print Multiplication table using do while loop
*/
#include <stdio.h>
int main()
{
int n, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
do
{
printf("%d x %d = %d\n", n, i, n * i);
i++;
} while (i <= 10);
return 0;
}
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.
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
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
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.
/*
* C Program to print Multiplication table using function
*/
#include <stdio.h>
void multiplicationTable(int num)
{
printf("Multiplication Table for %d:\n", num);
for (int i = 1; i <= 10; i++)
{
// Print the table using a suitable format
printf("%d x %d = %d\n", num, i, num * i);
}
}
int main(void)
{
int num;
/* Ask the user for a number and store it in a variable*/
puts("Enter a number : ");
scanf("%d", &num);
multiplicationTable(num);
}
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.
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
In this approach, calculate the multiplication table of a number using recursion.
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.
/*
* C Program to print Multiplication table using recursion
*/
#include <stdio.h>
int multiplicationTable(int num, int i)
{
if (i > 10)
{
return;
}
printf("%d x %d = %d\n", num, i, num * i);
multiplicationTable(num, i + 1);
}
int main(void)
{
int num;
// Ask the user for a number and store it in a variable
puts("Enter a number : ");
scanf("%d", &num);
multiplicationTable(num, 1);
}
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.
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”.
- Check C Books
- Apply for C Internship
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos