C Program to Print Multiplication Table Using While and Do While Loops – Step-by-Step Tutorial
In this tutorial, we'll learn how to print the multiplication table of a number using while and do-while loops in C. Knowing how to control loops is an important skill for programmers, especially when you need to generate sequences or process data repeatedly.
The main idea is simple: with a loop, you start from a number and keep multiplying and increasing it until you reach your limit. The while loop checks the condition first, and if it’s true, it runs the code inside. The do-while loop, on the other hand, runs the code at least once before checking the condition.
How to print a multiplication table with a while loop
In the first program, we ask the user to enter a number and set a variable j to 1. Then, as long as j is less than or equal to 10, the program prints the multiplication line and increases j by 1. This process continues until j becomes 11, and at that point, the loop stops. As a result, the program prints the multiplication table from 1 to 10.
How to print a multiplication table with a do-while loop
The second program asks the user to enter a number. It then uses a do-while loop to print the multiplication table starting from 1 up to 10. The loop runs at least once, printing the calculated result and then increasing j. It keeps doing this until j is greater than 10. So, if you enter 5, it will print the multiplication table for 5 from 1 to 10.
C Program Code to Print Multiplication Table Using While Loop
Output
Enter Any Number
5
Table of 5 is:
5 10 15 20 25 30 35 40 45 50
How This Program Works
When you run this program, it allocates memory for two integer variables: j, which starts at 1 to track our steps, and n, which holds the input number. The computer displays "Enter Any Number" on the screen and pauses at the scanf function until you type a value. If you enter 5, that value is saved directly into n, and the program prints the heading line "Table of 5 is:" to format the output.
Next, the execution path reaches the while loop and tests the condition j <= 10. Since j is currently 1, the test passes, and the program moves inside the loop block. It multiplies n * j (5 x 1), prints the result 5 on the screen, and encounters the j++ statement, which increases our counter to 2 before jumping straight back up to the loop check.
This cycle repeats smoothly as j climbs. On the next passes, the program verifies the condition, prints the calculated products like 10, 15, and 20, and increments the counter by 1 each time. The loop spins continuously until j prints 50 and reaches 11. At that moment, the condition becomes false, the loop stops, and the program prints a final new line to finish up cleanly.
C Program Code to Print Multiplication Table Using Do-While Loop
Output
Enter any Number.
5
Table of 5:
5 10 15 20 25 30 35 40 45 50
How This Program Works
When you run the program, it initializes the loop counter j to 1 and prompts you to enter a number. If you enter 5, that value is stored in the variable n, and the program prints the heading text "Table of 5:".
Next, the execution path enters the do block directly without checking any conditions upfront. It immediately multiplies n * j (5 x 1), prints the result 5 to the screen, and increments j to 2 using the j++ statement.
Only after completing this pass does the program check the condition while(j <= 10) at the bottom. Since 2 is less than 10, it jumps back to the top of the loop. This cycle repeats, printing the remaining products until j reaches 11, where the condition fails and the loop stops cleanly.
🚀 Practice Question
Try modifying this program to print:
- Odd and even numbers from 1 to N
- Sum of Odd numbers between range
Practice Questions
Try modifying these programs to expand your logic:
- The multiplication table in reverse order (from 10 down to 1).
- A multiplication table up to a user-defined limit N (like 5 x 15 or 5 x 20).
Frequently Asked Questions (FAQs)
Q: How does a C program print a multiplication table using a while loop?
A: To print a multiplication table using a while loop in C, the program takes a number input from the user and initializes a counter variable j to 1. The while loop runs as long as j is less than or equal to 10. Inside the loop, the program multiplies the input number by j, prints the formatted result, and increments j by 1 during each iteration.
Q: How do you generate a math table using a do-while loop in C?
A: Using a do-while loop, the program first executes the multiplication and printing statements inside the loop body for the initial value of the counter j (which starts at 1). After printing the row and incrementing j, the loop condition 'j <= 10' is evaluated at the bottom. The loop continues to execute until j exceeds 10.
Q: Why do we initialize the loop counter variable 'j' to 1 for tables?
A: Standard multiplication tables traditionally start by multiplying the target number by 1 and end at 10. Initializing the counter variable j to 1 ensures the table starts from the very first multiple rather than zero or a random garbage value.
Q: Can we print a multiplication table beyond 10 using these C loops?
A: Yes, you can easily change the termination limit. By replacing the hardcoded condition 'j <= 10' with a user-defined variable 'N' (like j <= N), the program can dynamically print the multiplication table up to any range the user desires.