Java Program to Print the Multiplication Table of a Number

In this Java program, we will learn how to print the multiplication table of a given number using a for loop.

Java Program

import java.util.Scanner;

public class MultiplicationTable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        for (int i = 1; i <= 10; i++) {
            System.out.println(num + " x " + i + " = " + (num * i));
        }
        sc.close();
    }
}

Output

Enter a number: 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

Explanation

The program first takes a number from the user using the Scanner class.

int num = sc.nextInt();

A for loop is then used to generate the multiplication table from 1 to 10:

for (int i = 1; i <= 10; i++)

Inside the loop, the number is multiplied by the current value of i:

num * i

The result is displayed using System.out.println().

Key Points

  • Scanner is used to read input from the user.
  • A for loop generates the multiplication table.
  • The table is printed from 1 to 10.
  • The multiplication result is calculated using the * operator.

Conclusion

This simple Java program demonstrates how to use user input, a for loop, and arithmetic multiplication to generate a multiplication table.

2. Java 8 Program

import java.util.Scanner;
import java.util.stream.IntStream;

public class MultiplicationTable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        IntStream.rangeClosed(1, 10)
                 .forEach(i -> System.out.println(
                         number + " x " + i + " = " + (number * i)
                 ));
        sc.close();
    }
}

Output

Enter a number: 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

Explanation

The program first accepts a number from the user using the Scanner class.

int number = sc.nextInt();

Java 8’s IntStream.rangeClosed() is then used to generate numbers from 1 to 10:

IntStream.rangeClosed(1, 10)

The forEach() method processes each number in the stream:

.forEach(i -> System.out.println(
    number + " x " + i + " = " + (number * i)
));

Here, the lambda expression i -> is a Java 8 feature. For every value of i, the program multiplies it by the input number and prints the result.

Key Points

  • IntStream is part of the Java 8 Stream API.
  • rangeClosed(1, 10) generates numbers from 1 through 10.
  • forEach() processes each value.
  • Lambda expressions are used to print the multiplication table.
  • The program is compatible with Java 8 and later versions.

Conclusion

This Java 8 program demonstrates how Streams and Lambda expressions can be used to generate a multiplication table in a concise and modern way.

Backend developer working with Java, Spring Boot, Microservices, NoSQL, and AWS. I love sharing knowledge, practical tips, and clean code practices to help others build scalable applications.

Leave a Reply

Your email address will not be published. Required fields are marked *