Table of Contents
C program to Generate multiplication table
In this article, we will discuss the concept of C program to Generate multiplication table.
In this program, we are going to learn how to generate a multiplication table in C programming language.
This is done using for loop , while loop , do-while loop , function and recursion
program to Generate multiplication table
Code to Generate multiplication table using for loop
In this program, we will display multiplication table of given number using for loop in C language.
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int num,i; printf("Enter an integer\n"); scanf("%d",&num); for(i=1; i<=10; i++){ printf("%d * %d = %d\n",num,i,num*i); } getch(); return 0; }
When the above code is executed,it produces the following result
Enter an integer 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
- integer variable num and i are declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using Scanf() function and store to the variable num
- Create a for loop of i from 1 to 10 and increase the value of i after every iteration by 1
- finally, the program displays the multiplication table using printf() function.
Code to Generate multiplication table using while loop
In this program, we will display multiplication table of given number using while loop in C language.
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int num,i; printf("Enter an integer\n"); scanf("%d",&num); i=1; while(i<=10){ printf("%d * %d = %d\n",num,i,num*i); i++; } getch(); return 0; }
When the above code is executed,it produces the following result
Enter an integer 15 15 * 1 = 15 15 * 2 = 30 15 * 3 = 45 15 * 4 = 60 15 * 5 = 75 15 * 6 = 90 15 * 7 = 105 15 * 8 = 120 15 * 9 = 135 15 * 10 = 150
- integer variable num and i are declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using Scanf() function and store to the variable num
- Create a while loop of i from 1 to 10 and increase the value of i after every iteration by 1
- finally, the program displays the multiplication table using printf() function.
Code to Generate multiplication table using do-while loop
In this program, we will display multiplication table of given number using do-while loop in C language.
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int num,i; printf("Enter an integer\n"); scanf("%d",&num); i=1; do{ printf("%d * %d = %d\n",num,i,num*i); i++; }while(i<=10); getch(); return 0; }
When the above code is executed,it produces the following result
Enter an integer 8 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 80
- integer variable num and i are declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using scanf() function and store to the variable num
- Create a do-while loop of i from 1 to 10 and increase the value of i after every iteration by 1
- finally, the program displays the multiplication table using printf() function.
Code to Generate multiplication table using function
In this program, we will display multiplication table of given number using function in C language.
Program 4
#include <stdio.h> #include <stdlib.h> void mulTable(int); int main() { int num; printf("Enter a positive number\n"); scanf("%d",&num); printf("\nMutiplication table for %d is\n",num); mulTable(num); getch(); return 0; } void mulTable(int num) { int count; for(count=1; count<=12; count++ ){ printf("%d x %d = %d\n",num,count,num*count); } }
When the above code is executed,it produces the following result
- Declare a function named as mulTable(int); with integer parameter
- integer variables num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using scanf() function and store the variable num.
- Define the function mulTable(int); to print mutiplication table
- Call the function to produce output
- finally, the program displays the multiplication table using printf) function.
Code to Generate multiplication table using recursion
In this program, we will display multiplication table of given number using recursion in C language
Program 5
#include <stdio.h> #include <stdlib.h> void mulTable(int,int); int main() { int num,i=1; printf("Enter a positive number\n"); scanf("%d",&num); printf("\nMutiplication table for %d is\n",num); mulTable(num,i); getch(); return 0; } void mulTable(int num,int i) { if(i>10){ return 0; } else { printf("%d*%d=%d\n",num,i,num*i); return mulTable(num,i+1); } }
When the above code is executed,it produces the following result
Suggested post
Similar post
Program to Display multiplication table in Java
Program to Display multiplication table in C