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

Show Hint

Push n only if it is even and has three digits. Then pop from the end of the list until it is empty.
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Plan the program:
We take 10 numbers in a loop. A number goes onto the stack only if it is a three-digit even integer. After the loop, we pop everything and print it.

Step 2: Test for a three-digit even number:
A three-digit integer lies between 100 and 999. An even integer leaves remainder 0 when divided by 2. We join both tests with and.

Step 3: Write the program:
stack = []
for i in range(10):
    n = int(input('Enter an integer: '))
    if 100 <= n <= 999 and n % 2 == 0:
        stack.append(n)
while len(stack) > 0:
    print(stack.pop(), end=' ')

append() is the push. pop() takes the top element, which is the last one pushed, so the order of printing is the reverse of the order of entry.

Step 4: Trace the example:
Inputs: 12, 31, 320, 457, 6, 92, 924, 220, 1, 218.
12, 31, 457, 6, 92 and 1 are not three-digit even numbers. 457 is three-digit but odd.
The stack becomes [320, 924, 220, 218]. Popping gives 218, 220, 924, 320. I ran the program on these inputs and got exactly this output.

Final Answer:
The program above prints 218 220 924 320 for the given input.
Was this answer helpful?
0
0