Program 1: Java program to print a multiplication table for any number using Scanner class.
//importing java packages
import java.util.Scanner;
public class MultiplicationTable1{
public static void main (String[] args){
//creating new Scanner
Scanner sc = new Scanner (System.in);
System.out.println ("Please enter an number:\n");
int num = sc.nextInt ();
System.out.println ("Multiplication Table of "+num+" is:\n");
System.out.println (num+" x 1 = "+(num * 1));
System.out.println (num+" x 2 = "+(num * 2));
System.out.println (num+" x 3 = "+(num * 3));
System.out.println (num+" x 4 = "+(num * 4));
System.out.println (num+" x 5 = "+(num * 5));
System.out.println (num+" x 6 = "+(num * 6));
System.out.println (num+" x 7 = "+(num * 7));
System.out.println (num+" x 8 = "+(num * 8));
System.out.println (num+" x 9 = "+(num * 9));
System.out.println (num+" x 10 = "+(num * 10));
System.out.println (num+" x 11 = "+(num * 11));
System.out.println (num+" x 12 = "+(num * 12));
}
}
Save this file as 'MultiplicationTable1.java'
Compile: $javac MultiplicationTable1.java
Execute: $java MultiplicationTable1
Output:
Please enter an number:
15
Multiplication Table of 15 is:
15 x 1 = 15
15 x 2 = 30
15 x 3 = 45
15 x 4 = 60
15 x 5 = 75
15 x 6 = 90
15 x 7 = 105
15 x 8 = 120
15 x 9 = 135
15 x 10 = 150
15 x 11 = 165
15 x 12 = 180
Program 2: Java program to print a multiplication table for any number using BufferedReader class.
//importing java packages
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class MultiplicationTable2{
public static void main (String[] args)
throws IOException{
//creating new InputStreamReader
InputStreamReader is = new InputStreamReader (System.in);
//creating new BufferedReader
BufferedReader br = new BufferedReader (is);
System.out.println ("Please enter an number:\n");
int num = Integer.parseInt (br.readLine ());
System.out.println ("Multiplication Table of "+num+" is:\n");
System.out.println (num+" x 1 = "+(num * 1));
System.out.println (num+" x 2 = "+(num * 2));
System.out.println (num+" x 3 = "+(num * 3));
System.out.println (num+" x 4 = "+(num * 4));
System.out.println (num+" x 5 = "+(num * 5));
System.out.println (num+" x 6 = "+(num * 6));
System.out.println (num+" x 7 = "+(num * 7));
System.out.println (num+" x 8 = "+(num * 8));
System.out.println (num+" x 9 = "+(num * 9));
System.out.println (num+" x 10 = "+(num * 10));
System.out.println (num+" x 11 = "+(num * 11));
System.out.println (num+" x 12 = "+(num * 12));
}
}
Save this file as 'MultiplicationTable2.java'
Compile: $javac MultiplicationTable2.java
Execute: $java MultiplicationTable2
Output:
Please enter an number:
12
Multiplication Table of 12 is:
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
12 x 11 = 132
12 x 12 = 144
Program 3: Java program to print a multiplication table for any number using printf command.
//importing java packages
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class MultiplicationTable3{
public static void main (String[] args)
throws IOException{
//creating new InputStreamReader
InputStreamReader is = new InputStreamReader (System.in);
//creating new BufferedReader
BufferedReader br = new BufferedReader (is);
System.out.println ("Please enter an number:\n");
int num = Integer.parseInt (br.readLine ());
System.out.printf ("Multiplication Table of %d is:\n", num);
System.out.printf ("%d x 1 = %d\n", num, (num * 1));
System.out.printf ("%d x 2 = %d\n", num, (num * 2));
System.out.printf ("%d x 3 = %d\n", num, (num * 3));
System.out.printf ("%d x 4 = %d\n", num, (num * 4));
System.out.printf ("%d x 5 = %d\n", num, (num * 5));
System.out.printf ("%d x 6 = %d\n", num, (num * 6));
System.out.printf ("%d x 7 = %d\n", num, (num * 7));
System.out.printf ("%d x 8 = %d\n", num, (num * 8));
System.out.printf ("%d x 9 = %d\n", num, (num * 9));
System.out.printf ("%d x 10 = %d\n", num, (num * 10));
System.out.printf ("%d x 11 = %d\n", num, (num * 11));
System.out.printf ("%d x 12 = %d\n", num, (num * 12));
}
}
Save this file as 'MultiplicationTable3.java'
Compile: $javac MultiplicationTable3.java
Execute: $java MultiplicationTable3
Output:
Please enter an number:
5
Multiplication Table of 5 is:
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
5 x 11 = 55
5 x 12 = 60

0 Comments