import java.util.Stack; public class MaxNumberInStack { public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); stack.push(10); stack.push(30); stack.push(50); stack.push(20); stack.push(40); System.out.println("Stack: " + stack); int maxNumber = findMaxNumberInStack(stack); System.out.println("Maximum number in the stack: " + maxNumber); } public static int findMaxNumberInStack(Stack<Integer> stack) { if (stack.isEmpty()) { throw new IllegalStateException("Stack is empty."); } int max = stack.peek(); // Initialize max with the top element for (Integer element : stack) { if (element > max) { max = element; } } return max; } }