Multiplication Table using Loops in C
To generate a multiplication table in C, we can use loops like for
, while
, or do-while
. By iterating through numbers and multiplying them with a given value, we can print a structured multiplication table. In this tutorial, we will cover multiple approaches using different loop constructs.
Examples to Generate Multiplication Tables
1. Generating a Multiplication Table Using a for
Loop
In this example, we will use a for
loop to generate a multiplication table for a given number up to 10.
main.c
#include <stdio.h>
int main() {
int num = 5; // Number for which we want to generate the table
// Loop from 1 to 10 and print the multiplication result
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
Explanation:
- We declare an integer variable
num
and assign it the value5
, which represents the number whose multiplication table we want to generate. - We use a
for
loop that runs from1
to10
, wherei
represents the multiplier. - Inside the loop, we use
printf()
to print the multiplication in the format5 x 1 = 5
,5 x 2 = 10
, etc. - Each iteration prints one row of the multiplication table.
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
2. Generating a Multiplication Table Using a while
Loop
In this example, we will generate a multiplication table using a while
loop instead of a for
loop.
main.c
#include <stdio.h>
int main() {
int num = 7, i = 1; // Initialize number and counter
// Loop while i is less than or equal to 10
while (i <= 10) {
printf("%d x %d = %d\n", num, i, num * i);
i++; // Increment counter
}
return 0;
}
Explanation:
- We declare an integer variable
num
with a value of7
. - We initialize a counter variable
i
to1
, which will be used to iterate through multipliers. - The
while
loop runs as long asi
is less than or equal to10
. - Inside the loop,
printf()
prints the multiplication result. - We increment
i
after each iteration to move to the next row.
Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
3. Generating a Multiplication Table Using a do-while
Loop
In this example, we will generate a multiplication table using a do-while
loop, ensuring at least one iteration is executed.
main.c
#include <stdio.h>
int main() {
int num = 9, i = 1; // Initialize number and counter
// Loop executes at least once
do {
printf("%d x %d = %d\n", num, i, num * i);
i++; // Increment counter
} while (i <= 10);
return 0;
}
Explanation:
- We declare an integer variable
num
with a value of9
. - We initialize the counter variable
i
to1
. - The
do
block executes at least once, printing the multiplication result. - The condition
while (i <= 10)
ensures repetition untili
exceeds 10.
Output:
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
Conclusion
We have demonstrated how to generate a multiplication table in C using three different loops: for
, while
, and do-while
. Each method iterates through multipliers from 1 to 10, printing the results in a structured format.