Table of Contents
Program for find product of two floating point numbers in Java
In this article, we will discuss the concept of Program for find product of two floating point numbers in Java
In this post, we are going to learn how to write a program to calculate product of two floating point numbers using different methods in Java program.
Code to calculate product of two floats
Code to calculate product of two floating point numbers
In this code, we will calculate product of two floating point numbers using variable declaration and initialization in Java language
Program 1
public class Product_Two_Floats{ public static void main(String args[]){ float firstNum=2.5f; float secondNum=3.4f;//float variable declaration float product= firstNum * secondNum;//calculate product System.out.println("The product is: "+product); //display result } }
When the above code is executed, it produces the following result
The product is: 8.5
Code to calculate product of two floating point numbers – using user input
In this code, we will calculate product of two floating point numbers using takes input from the user in Java language
Program 2
import java.util.Scanner; public class Product_Two_Floats_from_input{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //Ask input from the user System.out.println("Please Enter two floating numbers: "); //read the first float number float firstNum=sc.nextFloat(); //read the second float number float secondNum=sc.nextFloat();; System.out.println(); float product= firstNum * secondNum;//calculate product of two floats System.out.println("The product is: "+product); //display result } }
When the above code is executed, it produces the following result
Please Enter two floating numbers 2.6 3.2 The product is: 8.32
Code to calculate product of two floating point numbers – using method
In this code, we will calculate product of two floating point numbers using method in Java language
Program 3
import java.util.Scanner; class ProductFloatWithmethod{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number: "); float n1=scan.nextFloat();//get input from the user for num1 System.out.print("Enter the second number: "); float n2=scan.nextFloat();//get input from the user for num2 calcFloatproduct(n1,n2);//calling the method } static void calcFloatproduct(float num1,float num2){//method definition double product=num1*num2; System.out.print("product of"+num1+"and"+num2+" is: "+product); } }
When the above code is executed, it produces the following result
Enter the first number: 12.5 Enter the second number: 10.4 Product of 12.5 and 10.4 is: 130.0
Suggested for you
Data type and variable in Java
Class and main method explanation in Java
Variable types in Java language
Similar post
Java code to product of two numbers
C code to product of two numbers
C++ code to product of two numbers
Python code to product of two numbers
Java code to Calculate product of two floating point numbers
C code to Calculate product of two floating point numbers
C++ code to Calculate product of two floating point numbers
Python code to Calculate product of two floating point numbers