Using a `while` Loop to Print a Multiplication Table
The `while` loop is a control flow statement that allows code to be executed repeatedly based on a given condition. If you want to print the multiplication table of a given number using a `while` loop, here is how you can do it in C:
- #include <stdio.h>
- int main() {
- int number, i = 1;
- // Input the number for which you want to print the multiplication table
- printf("Enter a number: ");
- scanf("%d", &number);
- // Use a while loop to print the multiplication table
- printf("Multiplication Table of %d using while loop:\n", number);
- while (i <= 10) {
- printf("%d x %d = %d\n", number, i, number * i);
- i++;
- }
- return 0;
- }
Explanation:
- Input the Number: The program first asks the user to input a number for which the multiplication table will be generated.
- Initialize `i`: The variable `i` is initialized to 1, which will act as the multiplicand.
- While Loop Condition: As long as `i` is less than or equal to 10, the loop continues.
- Print the Result: Within the loop, the program prints the result of `number * i`.
- increment `i`: After each iteration, `i` is incremented by 1 to prepare for the next run.
Output Example:
- Enter a number: 7
- Multiplication Table of 7 using while loop:
- 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
Using a `for` Loop to Print a Multiplication Table
The `for` loop is another control flow statement providing a compact way to write loops. If you want to print the multiplication table of a number using a `for` loop, here is how you can do it in C:
- #include <stdio.h>
- int main() {
- int number;
- // Input the number for which you want to print the multiplication table
- printf("Enter a number: ");
- scanf("%d", &number);
- // Use a for loop to print the multiplication table
- printf("Multiplication Table of %d using for loop:\n", number);
- for (int i = 1; i <= 10; i++) {
- printf("%d x %d = %d\n", number, i, number * i);
- }
- return 0;
- }
Explanation:
1. input the Number: Just as in the example with the `while` loop, this program first asks the user to input a number.
2. For Loop Initialization: The `for` loop initializes `i` to 1, checks the condition `i <= 10`, and increments `i` on each iteration.
3. Print the Result: The program prints the result of `number * i` inside the loop.
Output:- Enter a number: 7
- Multiplication Table of 7 using for loop:
- 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
Key Differences Between a `while` Loop and a `for` Loop
Syntax: The syntax of the `for` loop is more succinct than that of the `while` loop. With the `for` loop, we can combine initialization, condition testing, and incrementation all into one line; whereas, in the `while` loop, these steps must be executed separately.
When to Use: Use a `while` loop when you don't know how many times the statement execution will be repeated. Use a `for` loop when you know the number of iterations in advance.
Both loops are powerful tools in the C language for handling repetitive work. By understanding how to use them to print a multiplication table, you can apply these methods to solve more complex problems in the future. Whether you choose a `while` loop or a `for` loop totally depends on your actual use case and coding style. I hope you have fun playing around with the code to understand the range of the multiplication table generated, or even combining the two loops under one program to enhance your understanding of them all. Happy programming!