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? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

11th Dec 2020, 11:38 AM
Anujkumar Jain
5 Answers
+ 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")
12th Dec 2020, 6:36 PM
Lothar
Lothar - avatar
+ 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)).
11th Dec 2020, 1:02 PM
rodwynnejones
rodwynnejones - avatar
+ 2
rodwynnejones "point taken!" ;D
11th Dec 2020, 5:14 PM
Brian
Brian - avatar
+ 1
Compare the integer value against the original value: If ((int)x == x) // is integer else // is floating point
11th Dec 2020, 12:29 PM
Brian
Brian - avatar
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.
11th Dec 2020, 12:26 PM
rodwynnejones
rodwynnejones - avatar