Skip to content
Related Articles

Related Articles

Check if a Float value is equivalent to an Integer value
  • Last Updated : 18 Jan, 2021

Given a floating-point number N, the task is to check if the value of N is equivalent to an integer or not. If found to be true, then print “YES”. Otherwise, print “NO”.

Examples:

Input: N = 1.5
Output: NO

Input: N = 1.0
Output: YES

Approach: The idea is to use the concept of Type Casting. Follow the steps below to solve the problem:



  • Initialize a variable, say X, to store the integer value of N.
  • Convert the value float value of N to integer and store it in X.
  • Finally, check if (N – X) > 0 or not. If found to be true, then print “NO”.
  • Otherwise, print “YES”.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if N is
// equivalent to an integer
bool isInteger(double N)
{
 
    // Convert float value
    // of N to integer
    int X = N;
 
    double temp2 = N - X;
 
    // If N is not equivalent
    // to any integer
    if (temp2 > 0) {
        return false;
    }
    return true;
}
 
// Driver Code
int main()
{
    double N = 1.5;
 
    if (isInteger(N)) {
 
        cout << "YES";
    }
    else {
 
        cout << "NO";
    }
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
class GFG
{
     
// Function to check if N is
// equivalent to an integer
static boolean isInteger(double N)
{
 
    // Convert float value
    // of N to integer
    int X = (int)N;
    double temp2 = N - X;
 
    // If N is not equivalent
    // to any integer
    if (temp2 > 0)
    {
        return false;
    }
    return true;
}
   
// Driver code
public static void main(String[] args)
{
    double N = 1.5;
    if (isInteger(N))
    {
        System.out.println("YES");
    }
    else
    {
        System.out.println("NO");
    }
}
}
 
// This code is contributed by susmitakundugoaldanga

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to check if N is
# equivalent to an integer
def isInteger(N):
   
    # Convert float value
    # of N to integer
    X = int(N)
 
    temp2 = N - X
 
    # If N is not equivalent
    # to any integer
    if (temp2 > 0):
        return False
         
    return True
 
# Driver Code
if __name__ == '__main__':
     
    N = 1.5
     
    if (isInteger(N)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to check if N is
// equivalent to an integer
static bool isint(double N)
{
     
    // Convert float value
    // of N to integer
    int X = (int)N;
     
    double temp2 = N - X;
 
    // If N is not equivalent
    // to any integer
    if (temp2 > 0)
    {
        return false;
    }
    return true;
}
   
// Driver code
public static void Main(String[] args)
{
    double N = 1.5;
     
    if (isint(N))
    {
        Console.WriteLine("YES");
    }
    else
    {
        Console.WriteLine("NO");
    }
}
}
 
// This code is contributed by 29AjayKumar

chevron_right


Output: 

NO

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :