In this tutorial, you will learn how to write a C program to print multiplication or we can say the table of any number using a while loop conditional statement. We will see various examples, algorithms, logics and program explanations to solve this problem in C programming language.
Required Knowledge
- C Programming Basics
- C Programming Operators
- C printf and Scanf
- While loop in C
Problem Statement
You have to Write a C program that will take a number ‘n’ as input from the user. After taking the input it should print the multiplication table of ‘n’ using a while loop. The program provides a solution to generate and print the multiplication table of any number. Below is the example for better understanding.
Example
Input:
Enter a number: 5
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
Algorithm
- Start the program.
- Declare the variables:
num
to store the input number andi
as a counter. - Read the value of
num
from the user. - Initialize
i
to 1. - Print the header message: “Multiplication table of num:”
- Enter the while loop with the condition
i <= 10
.- Calculate the product of
num
andi
. - Print the multiplication expression: “num x i = product”.
- Increment
i
by 1.
- Calculate the product of
- Exit the while loop.
- End the program.
Logic used to solve this problem
- Declare a variable to hold the input number.
- Prompt the user to enter a number.
- Read and store the entered number in the variable.
- Initialize a num variable to 1.
- Use a while loop that runs as long as the num is less than or equal to 10 (or any desired range).
- Inside the loop, calculate the multiplication of the input number with the num.
- Print the multiplication result.
- Increment the num by 1 in each iteration.
- Continue the loop until the num reaches the desired range.
- Finish the program.
C Program to print table of any number
#include <stdio.h>
int main() {
int num, i = 1;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
while (i <= 10) {
int product = num * i;
printf("%d x %d = %d\n", num, i, product);
i++;
}
return 0;
}
Output
Enter a number: 5
Multiplication table of 5:
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
Program Explanation
- The program takes input for the number whose multiplication table is to be printed.
- It initializes a variable
num
to 1, which represents the current multiplier. - The program enters a while loop with the condition
num <= 10
to iterate through multipliers from 1 to 10. - Inside the while loop, it calculates the product of the input number and the current multiplier.
- It prints the equation
number * multiplier = product
on the screen, wherenumber
is the input number,multiplier
is the current multiplier, andproduct
is the calculated product. - It then increments the value of
num
by 1 to move on to the next multiplier. - The loop continues until
num
exceeds the value of 10. - Once the while loop is complete, the program ends.
.
Conclusion
The C program successfully prints the table of any number entered by the user using a while loop. By iterating through the integers from 1 to 10 and calculating the product of the given number with each integer, it displays the multiplication table on the screen. I hope this tutorial helps you to understand the table program in C.