Write a Java Program to print multiplication table for any number
Objective
Write a Java program to print multiplication table for any given number “N”. Given an integer “N” generate its multiples. Each multiple must be printed on a new line in the form: N x i = result.
Constraints
2 ≤ N ≤ 10
Java Program to print multiplication table
The following program shows how to generate multiples using for loop.
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class MultiplicationTableSolution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(bufferedReader.readLine().trim()); if ( 2 <= N && N <= 10){ for (int i=1; i<=10; i++){ int result = N*i; System.out.println(N+" x "+i+" = "+result); } } bufferedReader.close(); } }
Sample Input
2
Output
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20
Print multiples using While Loop
The following program prints multiples using while loop.
public class MultiplicationTableSolution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(bufferedReader.readLine().trim()); if ( 2 <= N && N <= 20){ int i=1; while(i <= 10) { System.out.printf("%d * %d = %d \n", N, i, N* i); i++; } } bufferedReader.close(); } }
Input
3
Output
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30
You’ll also like:
- Python program to find difference between two given numbers
- Python Programs to Print Patterns – Pyramid, Triangle using Star
- Java Palindrome Program- Check String or Number is Palindrome
- Program to Check Given Number is Odd or Even
- Convert single digit number to double digits string
- Find the greatest of three numbers
- Most Common Element in an Unsorted Array
- Get Common Elements between Two Unsorted Arrays
- Implement Stack using Linked List
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not ‘str’ – Python3
- Extract numbers from a string in python
- Java String substring() example program
- Find which users belongs to a specific group in linux
- In-place merge sort instead of normal merge sort ?
- Check if Python Object is a Number ?
References