Here's a Python function that implements the required functionality:
def PUSH(Arr):
stack = []
for num in Arr:
if num % 5 == 0:
stack.append(num)
if stack:
print("Stack elements are:")
for element in stack:
print(element)
else:
print("No elements divisible by 5 found.")
Here's an explanation of how this function works:
- First, we initialize an empty list called stack which will serve as our stack.
- Next, we loop through each element in the Arr list using a for loop.
- Inside the loop, we use the % operator to check if the current number is divisible by 5. If it is, we append it to the stack list.
- Once we have finished looping through all the elements in Arr, we check if stack has any elements using an if statement. If it does, we print out each element in the stack using another for loop. If it doesn't, we print an appropriate error message.
- The function doesn't return anything, it simply prints the stack or an error message.
You can call this function with a list of numbers as an argument, like this:
PUSH([10, 20, 25, 30, 35, 40])
This will output:
Stack elements are:
10
20
25
30
35
40
If you call the function with a list that doesn't contain any numbers divisible by 5, you'll get an error message:
PUSH([1, 2, 3, 4, 6, 7])
This will output:
No elements divisible by 5 found.