Python program to check whether a number is integer or not using for loop
ADVERTISEMENTS
In this article, you will learn how to check whether a number is an integer or not using the for loop and while loop in the python programming language.
Examples
Input: 15
15 is an integer number.
Input: 15.3
15.3 is a floating-point number.
You should have knowledge of the following topics to understand these programs:
- Python
input()
function - Python
str()
function - Python
for
loop statement - Python
while
loop statement - Python
if-else
condition statement - Python
break
keyword - Python
print()
function
1. Python program to check whether a number is an integer or not using for loop
# Python program to check whether a number is an integer or not using for loop
print ("Enter a number event float or integer type: ", end="")
random_number, flag = str (input ()), False
for i in random_number:
if i == '.':
flag = True
break
if flag:
print (random_number, " is a floating-point number.")
else:
print (random_number, " is an integer number.")
Output
Enter a number event float or integer type: 15
15 is an integer number.
2. Python program to check whether a number is an integer or not using while loop
# Python program to check whether a number is an integer or not using while loop
print ("Enter a number event float or integer type: ", end="")
random_number, flag = str (input ()), False
l, i = len (random_number), 0
while i < l:
if random_number[i] == '.':
flag = True
break
i += 1
if flag:
print (random_number, " is a floating-point number.")
else:
print (random_number, " is an integer number.")
Output
Enter a number event float or integer type: 15.3
15.3 is a floating-point number.
Explanation
In this program, we have taken input 15.3
from the user and parsed this input as a string.
Meanwhile using for
loop or while
loop on this string we find the .
symbol to verify It's an integer or floating-point number.