In this post we will go through the basic java program to print table of a number :
Input :
Enter number : 5
Output :
Multiplication table of 5
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
Approach 1: using for loop :
import java.util.Scanner; public class MultiplicationTable { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter number :"); int num=s.nextInt(); System.out.println("Multiplication table of "+num); for(int i=1; i <= 10; i++) { System.out.println(num+" * "+i+" = "+num*i); } } }
Approach 2: using While loop :
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter number :"); int num=s.nextInt(); System.out.println("Multiplication table of "+num); int i = 1; while(i<=10) { System.out.println(num+" * "+i+" = "+num*i); i++; } } }
More java programs:
Java Program to Get Input from User
Addition of two numbers in java
Java Program to Reverse a Number
Java Program to Check a Number is Palindrome or Not