This post is about how we can do multiplication in java. Multiplication in java follows the basic syntax of mathematics to multiply two numbers. You can also multiply more than 2 numbers in java according to the scenario or need. Now Let's see how we can do multiplication of numbers in Java.
Objective:
Write a java program to multiply the numbers.
Sample Input:
int a = 4
int b = 2
Output = 8
Code
1. Simple Multiplication
public static void main(String args[]){
int a = 4;
int b = 2;
int c;
c = a*b;
System.out.println("Product of the Two Numbers is: " + c);
}
}
Output
8
2. Multiplication using Scanner Class
Here we are performing multiplication in Java using Scanner Class. Scanner class basically use for taking the input from the users. By using the scanner class we can enter the values according to our choice.
import java.util.Scanner;
public class Multiply{
public static void main(String args[]){
Scanner sc = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter the first Number");
int a = sc.nextInt();
System.out.println("Enter the first Number");
int b = sc.nextInt();
int c;
c = a*b;
System.out.println("Product of the Two Numbers is: " + c);
}
}
Output
Enter the first Number
5
Enter the first Number
3
Product of the Two Numbers is: 15
0 Comments