1.6K
In this tutorial, we will write a C Program to Generate Multiplication Table of a Given Number. An example program is shown below
#include<stdio.h>
int main(){
int num;
// Take the number as an input from the user
printf("Enter the value of number whose multiplication table is to be printed\n");
scanf("%d", &num);
printf("The multiplication table of %d is\n", num);
for (int i = 0; i < 10; i++)
{
printf("%d X %d = %d\n",num, i+1, (i+1)*num);
}
return 0;
}
- We have declared an integer variable “num” which will be used to store user input
- The “printf” function is used to print “Enter the value of number whose multiplication table is to be printed” at the run time and “/n” will break the line
- The “scanf” function is used to get input from user; the “%d” refer to an integer and “num” is the variable in which the user input will be stored
- The “printf” function is used to print “The multiplication table of %d is”, the value of the variable “num” will be printed at the place of “%d”
- The “for” loop is used to iterate for the given number of times. The “printf” function inside “for” loop is used to print the multiplication table. Every times “for” loops iterates it work print the given number, value of (i+1), and value of (i+1) will be multiplied by the given number. For example if the user input the number 4 then the output will be like:
5 X 1 = 5 5 X 2 = 10
And so on till the value of “i” reaches the value 10. There is another logic to iterate the for loop without adding the value “1” in the variable “i” which is shown below
for (int i = 1; i <= 10; i++) { printf("%d X %d = %d\n",num, i, i*num); }