Q:

Java program to compare float and double values

belongs to collection: Java Basic Programs

0

Java program to compare float and double values

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 floatdoublelong double, the values cannot be predicted exactly, these are depending on the number of bytes.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find the remainder without using m... >>
<< Java program to calculate the gratuity of an emplo...