Table of Contents
In this article, we will discuss the concept of Java Code Examples – Multiply Two Numbers in 5 Easy Ways
In this post, we are going to learn how to write a program to 5 Ways to Multiply of Two Numbers (With Examples) in the Java programming language.
here, I’ll give you separate short programs for each method so it’s easier to understand
The simplest and most common way to multiply two numbers
Program 1
// Multiplication of two numbers in Java
// Basic multiplication using * operator
class Basic_Mul {
public static void main(String[] args) {
int num1=12;
int num2=6;
System.out.println("product of two numbers: "+(num1*num2));
}
} When the above code is executed, it produces the following result
product of two numbers: 72
The simplest and most common way to multiply two numbers with user input
Program 2
// Multiplication of two numbers in Java
// Basic multiplication using user input
import java.util.Scanner;
class Basic_Multiply {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter first number: ");
int num1=sc.nextInt();
System.out.print("Enter second number: ");
int num2=sc.nextInt();
System.out.println("product: "+(num1*num2));
}
} When the above code is executed, it produces the following result
Enter first number: 4 Enter second number: 5 product: 20
Multiply two numbers using recursive method
Program 3
// Multiplication of two numbers in Java
// Multiplication using recursion
class Mul_Recursion {
public static void main(String[] args) {
int num1=7;
int num2=8;
int product=mul(num1,num2);
System.out.println("product: "+product);
}
public static int mul(int a, int b){
//recursive method
if(b==0){
return 0;
}else{
return a+mul(a,b-1);}
}
} When the above code is executed, it produces the following result
product: 56
Multiply two numbers using constructor
Program 4
// Multiplication of two numbers in Java
// Multiplication using constructor
class Mul_Cons {
private int result;
//Constructor that takes two nubers and multioly them
public Mul_Cons(int a, int b){
this.result=a*b;
}
//method for get the result
public int getResult(){
return result;
}
public static void main(String[] args) {
int num1=8;
int num2=10;
//create an object for class
Mul_Cons mul=new Mul_Cons(num1, num2);
//getting the result and print it
System.out.println("product of two numbers: "+(mul.getResult()));
}
} When the above code is executed, it produces the following result
product of two numbers: 80
Multiply two numbers using method with array
Program 5
// Multiplication of two numbers in Java
// Simple Multiplication using method with array
class MultiplyUsingArray {
public static void main(String[] args) {
int num1=12;
int num2=13;
int[] result=new int[1];
mul(num1,num2,result);
System.out.println("Product: "+result[0]);
}
public static void mul(int a, int b, int[] result){
result[0]=a*b;
}
} When the above code is executed, it produces the following result
Product: 156
Similar post
C++ program to multiply two numbers using 6 ways
C program to multiply two numbers using 6 ways
JavaScript program to multiply two numbers using 4 ways
PHP multiply two numbers using function
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
How to Divide two numbers in Java| 5 different ways In this article, we will…
5 Ways to Add Two Numbers in Java with User Input (Examples) In this article,…
This website uses cookies.