In: Computer Science
Suppose the interface and the class of stack already implemented, Write application program to
1- insert 100 numbers to the stack
2- Print the even numbers
3- Print the summation of the odd numbers
Java code
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Stack st = new Stack();
Stack temp = new
Stack();
Scanner scan = new
Scanner(System.in);
int num,sum = 0;
System.out.println("Enter 100
elements in stack");
for(int
i=0;i<100;i++){
System.out.print("Enter number : ");
num =
scan.nextInt();
st.push(num);
}
System.out.println("Even
numbers in stack are");
while(!st.isEmpty()){
num =
st.top();
st.pop();
if(num % 2
== 0)System.out.println(num);
sum = sum
+ num;
temp.push(num);
}
while(!temp.isEmpty()){
st.push(temp.top());
temp.pop();
}
System.out.println("Sum of all odd
numbers is " + sum);
scan.close();
}
}