In this post, we are going to write a C program to generate Multiplication table. Let’s see how to write it with a simple snippet first.
#include <stdio.h>
int main()
{
int table_no = 7;
int upto = 10;
int i;
for(i=1; i<=upto; ++i)
{
printf("%d * %d = %d\n", table_no, i, table_no*i);
}
return 0;
}The above snippet will print the 7 times table. An output of the above script is:
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70
Let’s write a complete C program to generate Multiplication table dynamically.
#include <stdio.h>
int main()
{
int table_no;
int upto;
int i;
//get the table_no
printf("Multiplication table of: ");
scanf("%d", &table_no);
//get the upto value
printf("Upto: ");
scanf("%d", &upto);
//lets loop till the value upto
for(i=1; i<=upto; ++i)
{
//now print the multiplication table
printf("%d * %d = %d\n", table_no, i, table_no*i);
}
return 0;
}The output of the program will be:
Multiplication table of: 12 Upto: 15 12 * 1 = 12 12 * 2 = 24 12 * 3 = 36 12 * 4 = 48 12 * 5 = 60 12 * 6 = 72 12 * 7 = 84 12 * 8 = 96 12 * 9 = 108 12 * 10 = 120 12 * 11 = 132 12 * 12 = 144 12 * 13 = 156 12 * 14 = 168 12 * 15 = 180
I hope this would have helped you learn some C programming language.