C Program to Generate Multiplication Table
Last Updated :
27 Mar, 2023
In this article, we are creating a multiplication table in c which is a basic program for printing tables in c. We are printing multiplication tables of the number up to a given range. We will use the concepts of looping and using a 2-D array to print a Multiplication Table.
Multiplication Table
A multiplication table is a table that shows the multiples of a number. A multiplication table is created by multiplying a constant number from 1 to a given range of numbers in repetition order.
Input:
num = 5
range = 10
Output:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Program to Print Multiplication Table in C
There are two approaches for printing tables in c
- Using loops and without storing them in an array
- Using loops and a 2-D array
1. Using loops and without storing them in an array
The idea is to use the concept of looping and directly print the multiplication table without storing them in an array.
Algorithm:
- Take the input of the number and the range of the multiplication table.
- Declare a variable to store the product.
- Use a for loop to directly multiply and print the Multiplication table.
C
#include <stdio.h>
void print_table( int range, int num)
{
int mul;
for ( int i = 1; i <= range; i++) {
mul = num * i;
printf ( "%d * %d = %d" , num, i, mul);
printf ( "\n" );
}
}
int main()
{
int range = 10;
int num = 5;
print_table(range, num);
return 0;
}
|
Output
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
- Time Complexity: O(n), as only for loop is required.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
2. Using loops and a 2-D array
Algorithm:
- Take the input of the number and the range of the multiplication table.
- Now use a for loop(variable “k”)is used to traverse the 2-D array, it iterates through 0 to the range.
- The first column stores the number (i.e arr[k][0] = num) .
- The second column stores the value to be multiplied (i.e arr[k][1] = k+1) .
- The third column stores the product (i.e arr[k][2] = arr[k][1] * arr[k][0] ) .
- Then use another loop to print the multiplication table.
C
#include <stdio.h>
void print_table( int range, int num)
{
int row, col;
row = range;
col = 3;
int arr[row][col];
for ( int k = 0; k < row; k++) {
arr[k][0] = num;
arr[k][1] = k + 1;
arr[k][2] = arr[k][1] * arr[k][0];
}
for ( int i = 0; i < row; i++) {
printf ( "%d * %d = %d" , arr[i][0], arr[i][1],
arr[i][2]);
printf ( "\n" );
}
}
int main()
{
int range = 10;
int num = 5;
print_table(range, num);
return 0;
}
|
Output
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Time Complexity: O(n).
Auxiliary Space: O(row *col), as a 2-D array is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...