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++
#include <bits/stdc++.h>
using namespace std;
bool isInteger( double N)
{
int X = N;
double temp2 = N - X;
if (temp2 > 0) {
return false ;
}
return true ;
}
int main()
{
double N = 1.5;
if (isInteger(N)) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isInteger( double N)
{
int X = ( int )N;
double temp2 = N - X;
if (temp2 > 0 )
{
return false ;
}
return true ;
}
public static void main(String[] args)
{
double N = 1.5 ;
if (isInteger(N))
{
System.out.println( "YES" );
}
else
{
System.out.println( "NO" );
}
}
}
|
Python3
def isInteger(N):
X = int (N)
temp2 = N - X
if (temp2 > 0 ):
return False
return True
if __name__ = = '__main__' :
N = 1.5
if (isInteger(N)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static bool isint( double N)
{
int X = ( int )N;
double temp2 = N - X;
if (temp2 > 0)
{
return false ;
}
return true ;
}
public static void Main(String[] args)
{
double N = 1.5;
if (isint(N))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
function isInteger(N)
{
let X = Math.floor(N);
let temp2 = N - X;
if (temp2 > 0)
{
return false ;
}
return true ;
}
let N = 1.5;
if (isInteger(N))
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Apr, 2021
Like Article
Save Article