Let’s write a simple C program to generate multiplication table. It’s fairly a simple program.
Here is a Short snippet to generate 7 times tables:
#include <stdio.h>
int main()
{
//define the values
int table_no = 7, upto = 10, i;
//loop through till upto limit and print the table
for (i=1; i<=upto; ++i){
printf("%d * %d = %d\n", table_no, i, table_no * i);
}
return 0;
}Output:
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 -------------------------------- Process exited after 0.1807 seconds with return value 0 Press any key to continue . . .
Now let’s convert this to a dynamic multiplication table generator, to do that we need to get the table_no and upto limit from user instead of defining the values.
Here is the rewritten of the above program to get the values from user:
#include <stdio.h>
int main()
{
int table_no, upto, i;
//get the table no from user
printf("Table no: ");
scanf("%d", &table_no);
//get the limit from user
printf("Upto: ");
scanf("%d", &upto);
//loop through the upto limit
for (i=1; i<=upto; ++i){
printf("%d * %d = %d\n", table_no, i, table_no * i);
}
return 0;
}Just go through the comments in the above program to understand it.
Example output of the above script:
Table no: 12 Upto: 12 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 -------------------------------- Process exited after 2.037 seconds with return value 0 Press any key to continue . . .
Enjoy Coding!