C Program to Create Multiplication Table
Multiplication table is a table of products of decimal sequence of numbers. For example, a multiplication table of 5 by 5 contains a top most row with values ranging from 1 to 5 and a left most row with values ranging from 1 to 5. The middle cells contains the algebraic product of the corresponding value in the top row and left most row. Following table is a 5x5 multiplication table,
1 | 2 | 3 | 4 | 5 | |
1 | 1 | 2 | 3 | 4 | 5 |
2 | 2 | 4 | 6 | 8 | 10 |
3 | 3 | 6 | 9 | 12 | 15 |
4 | 4 | 8 | 12 | 16 | 20 |
5 | 5 | 10 | 15 | 20 | 25 |
The following c program prints an n by n multiplication table. For example, if user inputs a value of 10, multiplication tables of all numbers from 1 to 10 are printed.
C Program to Create n by n Multiplication Table
We use the scanf and printf functions to generate multiplication table in c.
#include <stdio.h> // Multiplication table generator void printMultTable(int tableSize) { printf(" "); for(int i = 1; i<=tableSize;i++ ) { printf("%4d",i); } printf("\n"); for(int i=0;i<tableSize;i++) { printf("----"); } printf("--------\n"); for(int i = 1 ;i<=tableSize;i++) { // print left most column first printf("%4d |",i); for(int j=1;j<=tableSize;j++) { printf("%4d",i*j); } printf("\n"); } } int main() { printf("Please enter multiplication table size: "); int tableSize; scanf("%d",&tableSize); printMultTable(tableSize); return 0; }
The output for the program for an input of 6 is given below,
Please enter multiplication table size: 6 1 2 3 4 5 6 -------------------------------- 1 | 1 2 3 4 5 6 2 | 2 4 6 8 10 12 3 | 3 6 9 12 15 18 4 | 4 8 12 16 20 24 5 | 5 10 15 20 25 30 6 | 6 12 18 24 30 36
Students memorize the multiplication tables of numbers from 1 to 10. Using these tables it is possible to multiply large numbers without a calculator.
C Program to Generate Multiplication Table for a Number
The following c program prints the multiplication table of a given number. This table is actually a subset of the table we created earlier. Here we print the column corresponding to the given number in the first row.
#include <stdio.h> // Multiplication table for a number void printTable(int number) { for(int i = 1; i<=10;i++ ) { printf("%dx%d=%d\n",i,number,i*number); } } int main() { printf("Please enter a number from 1 to 10: "); int number; scanf("%d",&number); printTable(number); return 0; }
The output from the c program is given below,
Please enter a number from 1 to 10: 9 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 10x9=90