- How to print a multiplication table using while loop in C
- How to print a multiplication table using do-while loop in C
- The difference between while and do-while loops
- How to take user input and display the table
- Practice exercises to strengthen your understanding
Introduction
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.
1. Multiplication Table Using While Loop
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.
C Program Code to Print Multiplication Table Using While Loop
#include <stdio.h>
int main() {
int j = 1, n;
printf("Enter Any Number\n");
scanf("%d", &n);
printf("\n Table of %d is:", n);
while(j <= 10)
{
printf(" %d ", n * j);
j++;
}
printf("\n");
return 0;
}
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.
2. Multiplication Table Using Do-While Loop
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 Do-While Loop
#include <stdio.h>
int main() {
int j = 1, n;
printf("\nEnter any Number.");
scanf("%d", &n);
printf("Table of %d:\n", n);
do {
printf("%d ", n * j);
j++;
} while(j <= 10);
printf("\n");
return 0;
}
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 Exercises
š Exercise 1: Reverse Multiplication Table
Print the multiplication table in reverse order (from 10 down to 1).
Show Solution
#include <stdio.h>
int main() {
int j = 10, n;
printf("Enter Any Number\n");
scanf("%d", &n);
printf("Table of %d (Reverse):\n", n);
while(j >= 1) {
printf("%d ", n * j);
j--;
}
return 0;
}
š Exercise 2: Custom Range Table
Print a multiplication table up to a user-defined limit N (like 5 x 15 or 5 x 20).
Show Solution
#include <stdio.h>
int main() {
int j = 1, n, limit;
printf("Enter Number: ");
scanf("%d", &n);
printf("Enter Limit: ");
scanf("%d", &limit);
printf("Table of %d up to %d:\n", n, limit);
while(j <= limit) {
printf("%d ", n * j);
j++;
}
return 0;
}
Frequently Asked Questions
How does a C program print a multiplication table using a while loop?
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.
How do you generate a math table using a do-while loop in C?
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.
Why do we initialize the loop counter variable 'j' to 1 for tables?
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.
Can we print a multiplication table beyond 10 using these C loops?
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.
š” Tip: Always ensure your loop has a valid termination condition to avoid infinite loops.
š„ C While & Do-While Loop Programs (Practice Set 1)
š Practice the most important C while loop and do-while loop programs asked in exams and interviews. These beginner-to-advanced problems will help you master loop concepts quickly.
-
C Program to Print Odd Numbers from 1 to N (While & Do-While)
Learn how to print odd numbers using loops in C. A basic problem to understand loop iteration.
-
C Program to Print Even Numbers from 1 to N
Understand how to display even numbers using while and do-while loops in C.
-
C Program to Print Uppercase Alphabets (A to Z)
Print all uppercase letters using loops. Helps in understanding ASCII values and iteration.
-
C Program to Print Lowercase Alphabets (a to z)
Simple loop program to print lowercase alphabets in C.
-
C Program to Print Numbers from 1 to 10
Basic beginner example to understand number printing using loops.
-
C Program to Print Multiplication Table using While Loop
Take user input and generate a multiplication table. Common interview question.
-
C Program to Check Positive, Negative or Zero
Check number type continuously using loops until user exits.
-
C Program to Find Factorial using While Loop
Calculate factorial using loops. Important for coding interviews.
-
C Program to Find Sum of First N Natural Numbers
Compute sum from 1 to N using loop logic. Core beginner problem.
-
C Program to Print Prime Numbers from 1 to N
Learn prime number logic using nested loops in C programming.
-
C Program to Print Armstrong Numbers from 1 to N
Understand Armstrong number logic using loops and mathematical operations.
-
C Program to Print Leap Years using While Loop
Find leap years using conditions and loops. Useful for real-world logic building.
-
C Program to Reverse a Number using While Loop
Reverse digits of a number using loops. Frequently asked interview question.
š Practice/Assignment Set 2: C While & Do-While Loop Series Programs
1. C Program to Find Sum of Squares from 1 to N using While Loop
Learn how to calculate the sum of squares (1² + 2² + 3² + ... + N²) using while and do-while loops in C. This is a commonly asked logic-building problem in exams and coding interviews.
2. C Program to Find Sum of First N Natural Numbers using While Loop
Write a C program to calculate the sum of natural numbers (1 + 2 + 3 + ... + N) using loops. A basic and important program for beginners learning loop concepts.
3. C Program to Find Sum of Series (1/1! + 2/2! + ... + N/N!)
Practice a slightly advanced loop problem by computing the sum of factorial-based series using while or do-while loops. Helps strengthen logic and mathematical programming skills.
4. C Program to Find Sum of Harmonic Series (1 + 1/2 + 1/3 + ... + 1/N)
Learn how to calculate the harmonic series using loops in C. This program improves understanding of floating-point calculations and loop control.
5. C Program to Find Sum of Series (1 + 3²/3³ + 5²/5³ + ... up to N Terms)
Solve an advanced series-based problem using while loop logic. This type of question is frequently asked in competitive programming and technical interviews.
Other Tutorials
SQL FAQ
Java FAQ
Python FAQ