Q. Write a program to create a Stack for storing only odd numbers out of all the numbers entered by the user. Display the content of the Stack along with the largest odd number in the Stack. (Hint. Keep popping out the elements from stack and maintain the largest element retrieved so far in a variable. Repeat till Stack is empty)


Answer :- 


stack = [ ]

while True :
	num = int(input("Enter a Number :-"))
	if num % 2 != 0 :
		stack.append(num)
	ans = input("For quit enter (Y/N)")
	if ans == "Y" or ans == "y" :
		break

print("Your Stack :-",stack)
larg = stack.pop()
length = len( stack )
for i in range( length ) :
	num = stack.pop()
	if larg < num :
		larg = num 
		
print( "largest Number :-",larg )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post