Java Program to Generate Multiplication Table of the Input Number by@mayankvikash
388 reads

Java Program to Generate Multiplication Table of the Input Number

tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

The first method is long and time-consuming whereas the second one needs fewer lines of code but it is not beginners friendly. The second method requires basic knowledge of the while loop. In the first method, the first number is getting printed two times. The first number gets printed if you multiply it by 1, the number itself is printed by 1. The other method uses a while loop to print a multiplication table from 1 to 10 times with an increment. The program can be solved by two methods, one of which is very difficult.
featured image - Java Program to Generate Multiplication Table of the Input Number
Mayank Vikash HackerNoon profile picture

@mayankvikash

Mayank Vikash

Student with some knowledge of how technologies work.

Learn More
LEARN MORE ABOUT @MAYANKVIKASH'S EXPERTISE AND PLACE ON THE INTERNET.

Credibility

react to story with heart

Ever wondered how to take input from the user to calculate and display the multiplication table of the given number from 1 to 10?

Let's write a program in Java to perform this task.

This program can be solved by 2 methods, first one is long and time-consuming whereas the second one needs fewer lines of code but it is not beginners friendly.

Let us first start with the tough one then we will move towards the easy method.

As always, start with the importing statement.

import java.util.Scanner;

Public class and public static.

public  class  MultiplicationTable {
public  static  void  main(String[] args) {

Making Scanner object.

Scanner in = new Scanner(System.in);

Declaring the variable that we are going to use:

int n;

Asking the user to input the number.

System.out.println("Enter the number which multiplication table to display");

Taking input.

n = in.nextInt();

Just a mathematical operation for 10 times with an increment.

System.out.println(n*1);
System.out.println(n*2);
System.out.println(n*3);
System.out.println(n*4);
System.out.println(n*5);
System.out.println(n*6);
System.out.println(n*7);
System.out.println(n*8);
System.out.println(n*9);
System.out.println(n*10);

It will do the same thing, with just some more effort of writing so many lines of code.

Whole source code:

import  java.util.Scanner;
public  class  MultiplicationTable {
public  static  void  main(String[] args) {
Scanner  in = new  Scanner(System.in);
int n;
System.out.println("Enter the number which multiplication table to display");
n = in.nextInt();
System.out.println(n*1);
System.out.println(n*2);
System.out.println(n*3);
System.out.println(n*4);
System.out.println(n*5);
System.out.println(n*6);
System.out.println(n*7);
System.out.println(n*8);
System.out.println(n*9);
System.out.println(n*10);
	} 
}

Note: System.out.println(n); and System.out.println(n*1); is the same thing, since a number itself gets printed if you multiply it by 1.

Output from the first method:

Output from the first method

Output from the first method

I guess you have noticed it the first number is getting printed two times. I don't know why, probably there is something wrong with the compiler. I have commented out System.out.println(n); so that the first number does not get printed two times,

Now, let's make the program with the second and easier method.

Note: The second method requires basic knowledge of the while loop. it is recommended to learn about the while loop before proceeding.

Everything is the same I am just modifying the 10 System.out.println.

	while (i<=10) {
	i++; // i++ is below because the first number is getting print 2 times
	System.out.println(n*i);
}

Yeah, that's it. While loop makes life so easy.

You may also have noticed that usually the increment i++ is at the end of the program then why it is above the printing statement?

The reason is that the first number which is the number itself. is getting printed 2 times so to avoid that, the upgradation statement before the printing statement.

The first number is getting printed two times

The first number is getting printed two times

Source code:

import  java.util.Scanner;

public  class  MultiplicationTable {

public  static  void  main(String[] args) {

Scanner  in = new  Scanner(System.in);

int  n, i=1;

System.out.println("Enter the number which multiplication table to display");

n = in.nextInt();

while (i<=10) {

i++; // i++ is below because the first number is getting print 2 times

System.out.println(n*i);

}

}

}

Screenshot of the program

Screenshot of the program

Screenshot of the program

Let's write the program with for loop.


import  java.util.Scanner;

public  class  MultiplicationTable {

public  static  void  main(String[] args) {

Scanner  in = new  Scanner(System.in);

int  n, i;

System.out.println("Enter the number which multiplication table to display");

n = in.nextInt();

for (i = 1; i<=10; i++){
    System.out.println(n*i);
}


}

}

With Do while loop


import  java.util.Scanner;

public  class  MultiplicationTable {

public  static  void  main(String[] args) {

Scanner  in = new  Scanner(System.in);

int  n, i=1;

System.out.println("Enter the number which multiplication table to display");

n = in.nextInt();

    
do{
    
    System.out.println(n*i);
    i++;
}while(i<=10);


}

}

By the way, I have created a new site, check it out.


Also published here.

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa