A multiplication table displays the multiples of a given number from 1 to a specified range. In C, it can be generated using loops and can also be stored in a 2-D array before printing.
- The loop-based approach prints the table directly without storing it.
- The 2-D array approach stores the number, multiplier, and product before printing.
Example
For num = 5 and range = 10, the multiplication table is:
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
Methods to Generate a Multiplication Table
The multiplication table can be generated using the following approaches
Generate Multiplication Table Using a Loop
The simplest approach is to use a for loop to calculate and print each multiple directly without storing the results.
Steps
- Initialize the number and the range.
- Run a loop from 1 to the given range.
- Multiply the number by the current loop value.
- Print the number, multiplier, and product.
#include <stdio.h>
void print_table(int range, int num)
{
// Declaring a variable mul to store the product.
int mul;
// For loop to calculate the Multiplication table.
for (int i = 1; i <= range; i++) {
// To store the product.
mul = num * i;
// Printing the Multiplication Table.
printf("%d * %d = %d", num, i, mul);
// Proceeding to the next line.
printf("\n");
}
}
// Driver code
int main()
{
// The range of the
// Multiplication table
int range = 10;
// The number to calculate the
// Multiplication table
int num = 5;
// Calling the Function.
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
Explanation: The loop runs from 1 to range and calculates num * i in each iteration. Each product is printed immediately without storing the complete table.
Generate Multiplication Table Using a 2-D Array
The multiplication table can also be stored in a 2-D array before printing. Each row stores the number, multiplier, and their product.
Steps
- Initialize the number and range.
- Create a 2-D array with three columns.
- Store the number in the first column.
- Store the multiplier in the second column.
- Calculate and store the product in the third column.
- Traverse the array and print the stored values.
#include <stdio.h>
void print_table(int range, int num)
{
// Taking two integer variables row and column
int row, col;
// Initializing row with range of the multiplication
// table.
row = range;
// Initializing column with 3.
col = 3;
// Creating a 2-D array to calculate and store the
// Multiplication Table .
int arr[row][col];
// For loop to calculate the table
for (int k = 0; k < row; k++) {
// Storing the number in the first column.
arr[k][0] = num;
// Storing the value to be multiplied in the second
// column.
arr[k][1] = k + 1;
// Calculating and Storing the product in the third
// column.
arr[k][2] = arr[k][1] * arr[k][0];
}
// For loop to print the Multiplication table
for (int i = 0; i < row; i++) {
printf("%d * %d = %d", arr[i][0], arr[i][1],
arr[i][2]);
printf("\n");
}
}
// Driver code
int main()
{
// The range of the
// Multiplication table.
int range = 10;
// The number to calculate the
// Multiplication table.
int num = 5;
// Calling the Function.
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
Explanation
- arr ek 2-D array hai jo number, multiplier aur unke product ko store karta hai.
- Pehla for loop 1 se range tak ke multipliers aur corresponding products ko array mein store karta hai.
- Dusra for loop array se values lekar multiplication table print karta hai.
- arr[k][2] mein arr[k][0] * arr[k][1] ka product store hota hai.