0
Is there any way to find out whether the number entered by user is int or floating point number without using any loop in c?
5 Réponses
+ 8
with the code from Brian slightly modified there is an issue when:
x=10.0
print(f"{x} is of type {type(x)}") # stated x is "float"
if (int(x) == x):
print("is integer") # stated x is an integer
else:
print("is floating point")
+ 2
@Brian
I initially upvoted you post... then I realised..10 == 10.00.
...the 10.00 is still a floating point and the 10 is an int.
(unless I've miss- understood something (I wouldn't be surprised)).
+ 2
rodwynnejones "point taken!" ;D
+ 1
Compare the integer value against the original value:
If ((int)x == x)
// is integer
else
// is floating point
0
Bare in mind I'm not an expert...but what I would do is:-
Take the input as a char array. eg char num[10]..
..then check for the decimal point using strchr() from the string.h header, then use atoi() or atof() (there are other, just depends on what you want to convert to)......assign or output as necessary.