Print multiplication table java: In the previous article, we have seen Java Program to Find Product of Digits of a Number
In this article we are going to see how to generate multiplication table of a number using Java programming language.
Java Program to Generate Multiplication Table of a Number
Multiplication table java: Multiplication table of a number refers to multiplying a specific number generally with 1 to 10(or more) and producing the result.
For example:
The Number = 5 Multiplication Table: 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50
Let’s see different ways to generate multiplication table.
Method-1: Java Program to Generate Multiplication Table of a Number By Using For Loop
Approach:
- Create Scanner class object.
- Take input from the user for number for which the table is to be generated.
- Use the for loop starting from 1 till 10.
- Inside the loop, generate the multiplication table by multiplying the number with 1 to 10.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner class object Scanner sc = new Scanner(System.in); // taking input from user System.out.print("Enter number: "); int n = sc.nextInt(); System.out.println("Multiplication table of "+n+" :"); // calling method to print multiplication table printTable(n); } private static void printTable(int n) { // printing table using for loop for (int i = 1; i <= 10; i++) { System.out.println(n + " x " + i + " = " + n * i); } } }
Output: Enter number: 5 Multiplication table of 5 : 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Method-2: Java Program to Generate Multiplication Table of a Number By Using While Loop
Approach:
- Create Scanner class object.
- Take input from the user for number for which the table is to be generated.
- Initialize an iterator variable i to 1.
- Run the while loop until
i<=10
. - Inside the loop, print the number, iterator value and the product of number and iterator as shown below.
- Increment
i
by 1.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner class object Scanner sc = new Scanner(System.in); // taking input from user System.out.print("Enter number: "); int n = sc.nextInt(); System.out.println("Multiplication table of "+n+" :"); // calling method to print multiplication table printTable(n); } private static void printTable(int n) { // printing table using while loop int i = 1; while (i<=10) { System.out.println(n + " * " + i + " = " + (n*i)); i++; } } }
Output: Enter number: 6 Multiplication table of 6 : 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.
Related Java Programs: