Table of Contents
Program to display multiplication table in Java
In this article, we will discuss the concept of Program to Display multiplication table in Java.
In this program, we are going to learn how to generate a multiplication table.
This is done using for loop , while loop , do-while loop , method and recursion in Java language
Code to print multiplication table in Java
Java code to print multiplication table using for loop
Program 1
In this program, we will display the multiplication table using a for loop in Java language
//Java program to print multipication tabe //using for loop import java.util.Scanner; class Multipication_tabe{ public static void main(String args[]){ int num, i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("\nEnter the any number: "); num=scan.nextInt();//get input from the user for num for(i=1; i<=12; i++){ System.out.println(num+" * "+i+" = "+num*i); } } }
When the above code is executed, it produces the following result
- 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 Scanner class and store to the variable num
- Create a for loop of i from 1 to 12 and increase the value of i after every iteration by 1
- finally, the program displays the multiplication table using System.out.print() function.
Java code to print multiplication table using while loop
In this program, we can display the multiplication table using a while loop in Java anguage. here, the while loop is functioning until the condition of the while loop is true
Program 2
//Java program to print multipication tabe //using while loop import java.util.Scanner; class Multipication_tabe1{ public static void main(String args[]){ int num, i,j; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("\nEnter the any number: "); num=scan.nextInt();//get input from the user for num i=1; while(i<=12){ System.out.println(num+" * "+i+" = "+num*i); i++; } } }
When the above code is executed, it produces the following result
- 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 Scanner class and store to the variable num
- Create a while loop of i from 1 to 12 and increase the value of i after every iteration by 1
- finally, the program displays the multiplication table using System.out.print() function.
Java code to print multiplication table using do-while loop
In this program, we will display the multiplication table using a do-while loop in Java language. here, the while loop is functioning until the condition of the while loop is true
Program 3
//Java program to print multipication tabe //using do-while loop import java.util.Scanner; class Multipication_tabe2{ public static void main(String args[]){ int num, i,j; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("\nEnter the any number: "); num=scan.nextInt();//get input from the user for num i=1; do{ System.out.println(num+" * "+i+" = "+num*i); i++; }while(i<=12); } }
When the above code is executed, it produces the following result
- 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 Scanner class and store to the variable num
- Create a do-while loop of i from 1 to 12 and increase the value of i after every iteration by 1
- finally, the program displays the multiplication table using System.out.print() function.
Java code to print multiplication table using method
In this program, we will display the multiplication table using a method in Java language.
Program 4
//Java program to print multipication tabe //using method import java.util.Scanner; class Multipication_table_Method{ public static void main(String args[]){ int num, i,j; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("\nEnter the any number: "); num=scan.nextInt();//get input from the user for num mulTable(num); } public static void mulTable(int num){//method definision int i; for(i=1; i<=12; i++){ System.out.println(num+" * "+i+" = "+num*i); } } }
When the above code is executed, it produces the following result
- 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 Scanner class and store to the variable num
- define a method named as mulTable(int num) for print multiplication table
- Call the method to display multiplication table
Java code to print multiplication table using recursion
In this program, we will display the multiplication table using a recursion in Java language.
Program 5
Recursion is a technique in Java programming where a method calls itself recursively. This technique is used to substitution the loops to execute the same code multiple time
//Java program to print multipication tabe //using recursive method import java.util.Scanner; class Multipication_table_Recursion{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("\nEnter the any number: "); int num=scan.nextInt();//get input from the user for num mulTable(num,1); } public static void mulTable(int num,int count){ if(count<=12){ System.out.println(num+" * "+count+" = "+num*count); mulTable(num,++count); //call the function inside the same function } } }
When the above code is executed, it produces the following result
Suggested post
do-while loop in Java language
Similar post
Program to Display multiplication table in Java
Program to Display multiplication table in C