We all know what a multiplication table is, so let’s implement a Java program to print Multiplication Table.
Java program to print Multiplication Table:
import java.util.Scanner; class MultiplicationTable { public static void main(String args[]) { int n, c; System.out.println("Enter an integer to print it's multiplication table"); Scanner in = new Scanner(System.in); n = in.nextInt(); System.out.println("Multiplication table of "+n+" is :-"); for ( c = 1 ; c <= 10 ; c++ ) System.out.println(n+"*"+c+" = "+(n*c)); } }
OUTPUT:
# java MultiplicationTable Enter an integer to print it's multiplication table 7 Multiplication table of 7 is :- 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
We can also print the tables from a range, say we need to print table from 5 to 9. We can do it simply by using nested for loops. Lets see how to do it.
Java program to print Multiplication Table for a range:
import java.util.Scanner; class Tables { public static void main(String args[]) { int a, b, c, d; System.out.println("Enter range of numbers to print their multiplication table"); Scanner in = new Scanner(System.in); a = in.nextInt(); b = in.nextInt(); for (c = a; c <= b; c++) { System.out.println("Multiplication table of "+c); for (d = 1; d <= 10; d++) { System.out.println(c+"*"+d+" = "+(c*d)); } } } }
OUTPUT:
# javac Tables.java # java Tables Enter range of numbers to print their multiplication table 5 9 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 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 Multiplication table of 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 Multiplication table of 8 8*1 = 8 8*2 = 16 8*3 = 24 8*4 = 32 8*5 = 40 8*6 = 48 8*7 = 56 8*8 = 64 8*9 = 72 8*10 = 80 Multiplication table of 9 9*1 = 9 9*2 = 18 9*3 = 27 9*4 = 36 9*5 = 45 9*6 = 54 9*7 = 63 9*8 = 72 9*9 = 81 9*10 = 90