;

Java Program to Generate Multiplication Table


Tutorialsrack 19/04/2021 Java

In this java program, you’ll learn how to generate a multiplication table of a given number and print it on the display. We are doing this by using a for and a while loop in Java.

Example 1: Java Program to Generate Multiplication Table using For Loop

Example 1: Java Program to Generate Multiplication Table using For Loop
// Java Program to Generate Multiplication Table using For Loop

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		int number;
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter a Number: ");
		number = scanner.nextInt();
		scanner.close();

		for (int i = 1; i <= 10; ++i) {
			System.out.printf("%d * %d = %d \n", number, i, number * i);
		}
	}
}
Output

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

Example 2: Java Program to Generate Multiplication Table using While Loop

Example 2: Java Program to Generate Multiplication Table using While Loop
// Java Program to Generate Multiplication Table using While Loop

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		int number, i = 1;
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter a Number: ");
		number = scanner.nextInt();
		scanner.close();

		while (i <= 10) {
			System.out.printf("%d * %d = %d \n", number, i, number * i);
			i++;
		}
	}
}
Output

Enter a Number: 7

7 * 1 = 7 

7 * 2 = 14 

7 * 3 = 21 

7 * 4 = 28 

7 * 5 = 35 

7 * 6 = 42 

7 * 7 = 49 

7 * 8 = 56 

7 * 9 = 63 

7 * 10 = 70 


Related Posts



Comments

Recent Posts
Tags