Exercise:
Write a Java Program that multiplies two floating-point numbers.
Click Here to View the Solution!
public
class MultiplyTwoDecimalNumbers {
public static void main(String[] args) {
//variables of data type float are initialized
//to store the numbers
float first =
1.7f;
float second =
3.1f;
System.out.println("Numbers to be multiplied are: " + first + " " + second);
// new integer product is initialized
// the value is kept equal to the product of both numbers
float product = first * second;
System.out.println(
"The product is: " + product);
}
}
Click Here to View the Output!
Numbers to be multiplied are: 1.7 3.1 The product is: 5.27
Click Here to View the Explanation!
- In this program, Floating-point variables
first
andsecond
are initialized, and1.7f
and3.1f
are stored in them respectively. - It’s important to add
f
after the numbers to declare them asfloat
otherwise they will be assigned type
.double
- Another variable product is declared to store the product of the variables first and second using the
*
operator. - Finally
println()
function prints product on console screen.