In this tutorial, you will learn how to write a C program to generate multiplication table. We will see two programs in this article. In the first program, we are printing the multiplication table for the entered number. In the second example, we are displaying the multiplication table upto the specified range.
Example 1: Program to generate multiplication table
In this example, we are using for loop do print the multiplication table. User is asked to enter the integer and then program runs a loop from i= 1 to 10
and for every iteration of the loop, the printf()
function prints number * i
.
#include <stdio.h>
int main() {
int number, i;
printf("Enter an integer: ");
scanf("%d", &number);
printf("Multiplication table of %d: \n", number);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", number, i, number * i);
}
return 0;
}
Output:
Example 2: Program to display multiplication table till a specified range
In this example, we are not displaying the complete multiplication table. Here we are taking two inputs from the user, the number
and the range
. The range
represents the step till which the table needs to be printed.
In this example, we are also using a do-while loop that checks if the range entered by user is positive. If user enters negative range, this loop prompts the user again to enter the positive range. This loop will keep running and keep prompting user for the correct input until the user enters positive range.
#include <stdio.h>
int main() {
int number, i, range;
printf("Enter an integer: ");
scanf("%d", &number);
// If user doesn't enter positive range value
// repeat the prompt.
do{
printf("Enter the range: ");
scanf("%d", &range);
} while (range <= 0);
printf("Multiplication table of %d till range %d: \n", number, range);
for (i = 1; i <= range; ++i) {
printf("%d * %d = %d \n", number, i, number * i);
}
return 0;
}
Output: