java

Java code to Display multiplication table of a number in given range

Java code to Display multiplication table of a number in given range

In this article, we will discuss the concept of Java code to Display multiplication table of a number in given range

In this program, we are going to learn how to generate a multiplication table using several ways in Java language.

This is done using for loop , while loop , do-while loop and method in Java language

Code to Display multiplication table of a number in given range-using for loop

In this program, we will display the multiplication table of a number in given range using a for loop in Java language

Program 1

import java.util.Scanner;
public class Multiplication_Table{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
int num,range;
System.out.println("Enter the number: ");
num=sc.nextInt();
System.out.println("Enter the range: ");
range=sc.nextInt();
for(int i=1; i<=range; i++){
System.out.println(num+"x"+i+"="+num*i);

      }
    }

  }

When the above code is executed, it produces the following result

Enter the number:
9
Enter the range
11
9x`1=9
9x2=18
9x`3=27
9x4=36
9x`5=45
9x6=54
9x`7=63
9x8=72
9x`9=81
9x10=90
9x`11=99

  • integer variables num and range are declared.
  • The program asks input from the user
  • Then the user enters the input values for num and range
  • The program will read the input using Scanner class and store to the variable num and range respectively.
  • Create a for loop of i from 1 to 11 and increase the value of i after every iteration by 1
  • finally, the program displays the multiplication table using System.out.print() function.

 

Code to Display multiplication table of a number in given range-using while loop

In this program, we will display the multiplication table of a number in given range using a while loop in Java language

Program 2

import java.util.Scanner;
public class MultiplicationTableWhile{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
int num,range;
System.out.println("Enter the number: ");
num=sc.nextInt();
System.out.println("Enter the range: ");
range=sc.nextInt();
int i=1;
while( i<=range){
System.out.println(num+"x"+i+"="+num*i);
i++;
      }
    }

  }

When the above code is executed it produces the following result

Enter the number
5
Enter the range
9
5x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
5x7=35
5x8=40
5x9=45
 
  • integer variables num and range are declared.
  • The program asks input from the user
  • Then the user enters the input values for num and range
  • The program will read the input using Scanner class and store to the variable num and range respectively.
  • Create a while loop of i from 1 to 9 and increase the value of i after every iteration by 1
  • finally, the program displays the multiplication table using System.out.print() function.

 

Code to Display multiplication table of a number in given range-using do-while loop

In this program, we will display the multiplication table of a number in given range using a do-while loop in Java language

Program 3

import java.util.Scanner;
public class MultiplicationTableDoWhile{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
int num,range;
System.out.println("Enter the number: ");
num=sc.nextInt();
System.out.println("Enter the range: ");
range=sc.nextInt();
int i=1;
do{
System.out.println(num+"x"+i+"="+num*i);
i++;
      }while( i<=range);
    }

  }

When the above code is executed it produces the following result

Enter the number
6
Enter the range
9
6x1=6
6x2=12
6x3=18
6x4=24
6x5=30
6x6=35
6x7=40
6x8=45
6x9=54
6x10=60

 

  • integer variables num and range are declared.
  • The program asks input from the user
  • Then the user enters the input values for num and range
  • The program will read the input using Scanner class and store to the variable num and range respectively.
  • 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 System.out.print() function.

 

Code to Display multiplication table of a number in given range-using method

In this program, we will display the multiplication table of a number in given range using a method in Java language

Program 4

//Java program to print multipication tabe
//using method
import java.util.Scanner;
class MultipicationTableMethod{
public static void main(String args[]){
int num,range;
    Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  System.out.print("Enter the any number: ");
  num=scan.nextInt();//get input from the user for num
  System.out.print("Enter the range: ");
    range=scan.nextInt();
  mulTable(num,range);
}

public static void mulTable(int num,int range){
for(int i=1; i<=range; i++){
System.out.println(num+"x"+i+"="+num*i);
}
}
}

When the above code is executed it produces the following result

Enter the any number:7
Enter the range:  9
7x1=7
7x2=14
7x3=21
7x4=28
7x5=35
7x6=42
7x7=49
7x8=56
7x9=63


  • integer variables num and range are declared.
  • The program asks input from the user
  • Then the user enters the input values for num and range
  • The program will read the input using Scanner class and store to the variable num and range respectively.
  • define a method named as mulTable(int num) for print multiplication table
  • Call the method to display multiplication table
  • finally, the program displays the multiplication table

 

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.