Java Stacks question DO NOT ANSWER THIS QUESTION WITH ANY S

Java --> Stacks question

DO NOT ANSWER THIS QUESTION WITH ANY SCREENSHOTS/PICTURES, TYPE IT OUT NEATLY. Or I will downvote you for wasting my question. Here it is:

--------------------------------------------------------------

What does the following code fragment print when N is 50 ? Give a high-level description of what it does when presented with a positive integer N. (A high level description is not simply translating the code into English, but rather stating the purpose/function of the code as a whole.)

Stack<Integer> S = new Stack<Integer>();

while(N > 0) {

    S.push(N % 2);

    N = N / 2;

}

for (int d : S)

System.out.print(d);

Solution

In while loop we will take modulo of number and puss it to stack like

50%2=0 and puss it to stack N=50/2=25

25%2=1 and puss it to stack and N=25/2=12

12%2=0 and puss it to stack and N=12/2=6

6%2=0 and puss it to stack and N=6/2=3

3%2=1and puss it to stack and N=3/2=1

1%2=1 and puss it to stack and N=1/2=0 and while loop terminated

and S={0,1,0,0,1,1}

and for loop will be printed Stack element
so Output is {0,1,0,0,1,1}


Java --> Stacks question DO NOT ANSWER THIS QUESTION WITH ANY SCREENSHOTS/PICTURES, TYPE IT OUT NEATLY. Or I will downvote you for wasting my question. Here

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site