One real number n is given. Check if it is an integer.
Input. One real number n.
Output. Print “Ok” if
n is an integer, and “No” otherwise.
Sample input 1 |
Sample output 1 |
7.000 |
Ok |
|
|
Sample input 2 |
Sample output 2 |
-21.121 |
No |
mathematics
The number n
is an integer if its integer part matches the number itself. That is, if the
equality floor(n) = n holds.
Algorithm
implementation
Read the real number n.
scanf("%lf", &n);
Ñompare the number n with its integer part. If
these values are equal, then n is an integer.
if (floor(n) == n) puts("Ok");
else puts("No");
Java implementation
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
double n = con.nextDouble();
if (Math.floor(n) == n)
System.out.println("Ok");
else
System.out.println("No");
con.close();
}
}
Python implementation
import math
Read the real number n.
n = float(input())
Ñompare the number n with its integer part. If
these values are equal, then n is an integer.
if math.floor(n) == n: print("Ok")
else: print("No")
Python implementation –
is_integer
Read the real number n.
n = float(input())
The method is_integer() checks whether the given floating-point number represents an integer
value (its fractional part is zero).
if n.is_integer(): print("Ok")
else: print("No")