C Program to Generate Multiplication Table of a Given Number

140 views

In this tutorial, we will write a program for multiplication of tables of a given number in C language. 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; }
Code language: C/AL (cal)
  • 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);
     }

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Enable Notifications    OK No thanks