Java Program to perform multiplication of two numbers – 5 ways
Java supports different types of Operators. Operators can be classified into numbers of categories: Arithmetic operators, Bitwise operators, Logical Operators, Conditional operators, Assignment Operators, Relational operators, Special operators, Increment Operators, Decrement operators. Java Program to perform multiplication of two numbers – 5 ways are given below.
Arithmetic Operators:
Arithmetic operators are addition, substraction, multiplication, division, and modulus. The symbol modulus (%) which divides one operand by another and returns the remainder as its output.
The following table lists the arithmetic operators:
Operator | Meaning |
---|---|
+ | Addition or unary plus |
– | Substraction or unary minus |
* | Multiplication |
/ | Division |
% | Modulus |
The following simple program demonstrates the arithmetic operators.
1. Multiplication of 2 numbers using Direct method :
Program:
import java.io.*; import java.lang.*; class Multiplication { public static void main(String args[]) { int x= 5; int y= 5; int z= x*y; System.out.println("The Multiplication of 2 number is "+z); } }
Output:
The Multiplication of 2 number is 25
2. Multiplication of two numbers using Scanner Class:
Program:
import java.io.*; import java.lang.*; import java.util.*; class Multiplication { public static void main(String args[]) { Scanner c= new Scanner(System.in); System.out.println("Enter value of 'x'"); int x= c.nextInt(); System.out.println("Enter value of 'y'"); int y= c.nextInt(); int z= x*y; System.out.println("The multiplication of 2 number is"+z); } }
Output:
Enter value of 'x' 10 Enter value of 'y' 2 The multipliction of 2 number is 20
3. Multiplication of 2 numbers using Buffer Reader Class:
Program:
import java.io.*; import java.lang.*; class Multiplication { public static void main(String args[]) throws Exception { int a, b, c; BufferedReader br= new BufferedReader(new inputStreamReader(System.in)); System.out.println("Enter the value of a"); a=Integer.parseInt(br.readLine()); System.out.println("Enter the value of b"); b=Integer.parseInt(br.readLine()); c=a*b; System.out.println("The multiplication of two number is "+c); } }
Output:
Enter the value of a 10 Enter the value of b 5 The multiplication of two number is 50
4. Multipliction of two number without using * operator:
Program:
import java.io.*; import java.util.*; class Multiplication{ public static void main(String args[]) { Scanner sc= new Scanner(System.in); System.out.println("Enter the 1st number"); int a=sc.nextInt(); System.out.println("Enter the 2nd number"); int b=sc.nextInt(); int c=0; for(int i=0;i<b;i++) { c=c+a; } System.out.println("The Multiplication of two numbers is "+c); } }
Output:
Enter the 1st number 100 Enter the 2nd number 2 The Multiplication of two numbers is 200
5. Multiplication of two number using command line Arguments
Program :
import java.io.*; import java.util.*; Class Multiplication { public static void main(String args[]) { int m1 =Integer.parseInt(args[0]); int m2 =Integer.parseInt(args[1]); System.out.println(m1); System.out.println(m2); int m3=m1*m2; System.out.println("The multiplication of two number is "+m3); } }
Compile:
javac Multiplication.java
Run:
java Multiplication 5 5 25
Find the Output of the following Program:
Exercise 1:
import java.io.*; import java.lang.*; class Multiplication { public static void main(String args[]) { Scanner c= new Scanner(System.in); System.out.println("Enter value of 'x'"); float x= c.nextFloat(); System.out.println("Enter value of 'y'"); float y= c.nextFloat(); float z= x*y; System.out.println("The Multiplication of 2 number is"+z); } }
Exercise 2:
import java.io.*; import java.lang.*; class Multiplication { public static void main(String args[]) { Scanner c= new Scanner(System.in); System.out.println("Enter value of 'x'"); double x= c.nextDouble(); System.out.println("Enter value of 'y'"); double y= c.nextDouble(); double z= x*y; System.out.println("The Multiplication of 2 number is"+z); } }
Recommended Post:
1. Java program to print “Hello World” Message.
2. Java program to print Addition And Substraction of two numbers in different ways.