Question #242040

what will be the output of the following code? Rough work must be done as well.

int[] values={1,3,5,7,9,11,13,15,17,19};

    Stack s=new Stack();

    for(int i=0;i<values.length;i++)

    {

        s.push(values[i]);

    }

    int n=25;

    for(int i=0;i<4;i++)

    {

        n+=s.pop();

    }

    for(int i=0;i<2;i++)

    {

        n-=s.pop();

    }

    cout<<"\n"<<endl;


Expert's answer

The stack operates in LIFO(Last In First Out) manner.
Therefore, the four element to be removed from the stack will be 19, 17, 15, and1 3.
 These elements will be added to to 25, i.e 25 + 19 + 17 + 15 + 13 = 89

89 - (11 + 9)  = 69
Answer is = 69
LATEST TUTORIALS
APPROVED BY CLIENTS