Home »
Java Programs »
Java Basic Programs
Java program to multiply two numbers using plus (+) operator
Given two numbers, write a Java program to multiply two numbers using plus "+" operator.
Submitted by Nidhi, on February 23, 2022
Problem statement
In this program, we will read two integer numbers and calculate the multiplication of input numbers using the "+" operator.
Source Code
The source code to multiply two numbers using the plus "+" operator is given below. The given program is compiled and executed successfully.
// Java program to multiply two numbers
// using plus "+" operator
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
int mul = 0;
Scanner myObj = new Scanner(System.in);
System.out.printf("Enter first number: ");
num1 = myObj.nextInt();
System.out.printf("Enter second number: ");
num2 = myObj.nextInt();
for (int loop = 1; loop <= num2; loop++)
mul += num1;
System.out.printf("Multiplication of %d and %d is: %d\n", num1, num2, mul);
}
}
Output
Enter first number: 4
Enter second number: 5
Multiplication of 4 and 5 is: 20
Explanation
In the above program, we imported the "java.util.Scanner" package to read input from the user. And, we created a public class Main. It contains a static method main().
The main() method is an entry point for the program. And, we read two integer numbers from the user using the nextInt() method and calculated the multiplication of input numbers using the "+" operator with "for" loop and printed the result.
Java Basic Programs »