ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.