Hi in this article you will learn about a Python program that keeps accepting numbers from the user and prints if the given number is even number or not until zero is entered.
let's try to use the while loop for performing the above operation.
Write a Python Program to Get Input From The User Using a While Loop And Print if it is an Even Number or Not Until 0 is Not Pressed
Use the while loop with the true Boolean value, this will provide the developer with an infinite while loop. now inside The Infinite while loop ask the user to enter a number.
Use the conditional if statement to check if the number is equals to zero use the break statement to exit The Infinite while loop.
Use the elif block to check if the number is an even number by performing the modulus operation of the given number by 2. If the reminder of the modulus operation is 0 then print the number is an even number. use the else block to print the number is not an even number as shown below.
while True:
number = int(input("Enter a Number : "))
if number == 0:
break
elif number % 2 == 0:
print("Given Number is an Even Number ")
else:
print("Given Number is not an Even Number ")
Python Program
========================
while True:
number = int(input("Enter a Number : "))
if number == 0:
break
elif number % 2 == 0:
print("Given Number is an Even Number ")
else:
print("Given Number is not an Even Number ")
Output
==================
Enter a Number : 23
Given Number is not an Even Number
Enter a Number : 45
Given Number is not an Even Number
Enter a Number : 22
Given Number is an Even Number
Enter a Number : 90
Given Number is an Even Number
Enter a Number : 0
Output 2
================
Enter a Number : 12
Given Number is an Even Number
Enter a Number : 9
Given Number is not an Even Number
Enter a Number : 25
Given Number is not an Even Number
Enter a Number : 47
Given Number is not an Even Number
Enter a Number : 89
Given Number is not an Even Number
Enter a Number : 34
Given Number is an Even Number
Enter a Number : 0
Conclusion
================
The above python script uses the infinite while loop that keeps accepting the number from the user and checks if the number is an even number or not until zero is pressed.
Commented down below if you have any queries regarding the above Python program
0 Comments