Advertisements
Advertisements
Question
Write a Python program to accept 10 integers from the user. If the entered number is a three-digit even integer, push it onto a stack. After all inputs are taken, pop all the three-digit even integers from the stack and display them. For example, if the user enters 12, 31, 320, 457, 6, 92, 924, 220, 1, 218, then the stack should contain:
320, 924, 220, 218
and the output of the program should be:
218 220 924 320
Code Writing
Advertisements
Solution
# Initialize an empty stack
stack = []
# Accept 10 integers from the user
for i in range(10):
num = int(input("Enter an integer: "))
# Check if number is 3-digit (100-999) and even
if 100 <= num <= 999 and num % 2 == 0:
stack.append(num)
# Pop and display all elements from the stack
print("The output is:")
while len(stack) > 0:
print(stack.pop(), end=" ")shaalaa.com
Is there an error in this question or solution?
