In programming, it is often necessary to print the multiplication table of a given number. In this program, we will write a C program that prompts the user to enter a number and prints its multiplication table from 1 to 10.
Problem Statement
The problem statement is to write a C program that prompts the user to enter a number and prints its multiplication table from 1 to 10.
Solution
Here is the C program to print the multiplication table:
#include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); printf("\nMultiplication Table of %d\n", number); printf("--------------------------\n"); for (int i = 1; i <= 10; i++) { printf("%d x %d = %d\n", number, i, number * i); } return 0; }
Explanation
First, we include the stdio.h
header file which is used for input/output functions. Then, we define the main()
function as the starting point of our program.
We declare an integer variable number
which will be used to store the number entered by the user.
Next, we prompt the user to enter a number using the printf()
function. The scanf()
function is used to read in the number entered by the user. The first parameter to scanf()
is the format string “%d” which specifies that the input should be read in as an integer. The second parameter is the address of the number
variable where the input should be stored.
We then print the multiplication table heading using the printf()
function.
We use a for
loop to iterate through the numbers from 1 to 10. Within the loop, we print the multiplication table for the entered number using the printf()
function. The first parameter to printf()
is the format string “%d x %d = %d” which specifies that we want to print the number, the iteration variable, and their product. The second parameter is the entered number, the third parameter is the iteration variable, and the fourth parameter is the product of the two.
Finally, we return 0 to indicate successful program execution.
Conclusion
In this tutorial, we learned how to write a C program to print the multiplication table of a given number. We used a for
loop to iterate through the numbers from 1 to 10 and printed their product with the entered number using the printf()
function.