Java Program to Multiply Two Floating Point Numbers
We can find the product of two floating point numbers easily in Java.
Let’s see 2 different methods to do it.
Method-1 : Multiplication using the ‘*’ operator
We can find the product easily by simply using the ‘*’ operator
. It takes two operands and multiplies them.
Below program is implementation code.
import java.util.Scanner;
class multiply
{
public static void main(String args[])
{
System.out.println("Enter two floating-point numbers");
//Scanner class to take input
Scanner scan = new Scanner(System.in);
float num1 = scan.nextFloat(), num2 = scan.nextFloat();
//Multiplying both the operands to find the product
float product = num1 * num2;
//Printing the result
System.out.println("The product of "+num1+" and "+num2+" is "+product);
}
}
Output:
Enter two numbers
15.3
63.1
The product of 15.3 and 63.1 is 965.43
Method-2 : Multiplication using a user defined method
We can multiply two floating point numbers using a user defined method.
The process is completely same, here we only create a user defined method and we will put the same logic inside the user defined method. We need to call the method to get the result.
Below program is implementation code.
import java.util.Scanner;
class multiply
{
public static float prod(float num1, float num2)
{
//This user defined function takes two numbers as input and returns the product
float product = num1*num2;
return product;
}
public static void main(String args[])
{
//static initialization (f is used to signify the compiler that it is a floating
//point number and not double)
float num1 = 15.3f, num2 = 63.1f;
//Calling the user defined function prod() and passing num1 and num2 into it
//then printing the result
System.out.println("The product of "+num1+" and "+num2+" is "+prod(num1,num2));
}
}
Output:
The product of 15.3 and 63.1 is 965.43
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Basic Programs: