java

Program to Display multiplication table in Java

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

Print multiplication table

 

  • 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

print multiplication table
  • 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

print multiplication table

 

  • 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

Print multiplication table
  • 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

Multiplication table in Java

 

Suggested post

for loop in Java language

while loop in Java language

do-while loop in Java language

method in Java language

Operator in Java language

Data type in Java language

 

Similar post

Program to Display multiplication table in Java

Program to Display multiplication table in C

Program to Display multiplication table in C++

Program to Display multiplication table in Python

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.