Generate Multiplication Table
Course- C >
Example on how to generate the multiplication table of a number (entered by the user) using for loop.
The program takes an integer input from the user and generates the multiplication table up to 10.
Example #1: Multiplication Table Up to 10
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}
Output
Enter an integer: 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90
Here's a little modification of the above program to generate the multiplication table up to n (where n is a positive integer entered by the user).
Example #2: Multiplication Table Up to range (entered by the user)
#include <stdio.h>
int main()
{
int n, i, range;
printf("Enter an integer, which you want to generate multiplication table of: ");
scanf("%d",&n);
printf("Enter the range of the multiplication table: ");
scanf("%d", &range);
for(i=1; i <= range; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}
Output
Enter an integer, which you want to generate multiplication table of: 12 Enter the range of the multiplication table: 8 12 * 1 = 12 12 * 2 = 24 12 * 3 = 36 12 * 4 = 48 12 * 5 = 60 12 * 6 = 72 12 * 7 = 84 12 * 8 = 96