Java program to compare float and double values

Given a float and a double value, write a Java program to compare float and double values.
Submitted by Nidhi, on February 24, 2022

Problem Solution:

In this program, we will create a variable of float type and double type with the same values. Then we will compare both values and print the appropriate message.

Program/Source Code:

The source code to compare float and double values is given below. The given program is compiled and executed successfully.

// Java program to compare 
// float and double values

public class Main {
  public static void main(String[] args) {
    float num1 = 15.2F;
    double num2 = 15.2;

    if (num1 == num2)
      System.out.printf("Both values are equal.");
    else
      System.out.printf("Both values are not equal.");
  }
}

Output:

Both values are not equal.

Explanation:

In the above program, we created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we created two variables of float and double types respectively with the same value. Then we compared the value of both variables and printed the "Both values are not equal.".

Note: In floating-point numbers like float, double, long double, the values cannot be predicted exactly, these are depending on the number of bytes.

Java Basic Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.