C++, what is the output of the following code. //suppose that the input is: 31,
ID: 3860096 • Letter: C
Question
C++, what is the output of the following code.
//suppose that the input is: 31, 47, 86, 39, 62, 71, 15, 63
stackType<int> stack(50);
int num;
while (cin){
if (!stack.isFullStack())
if (num % 2 == 0 || num % 3 == 0)
stack.push(num);
else if (!stack.isEmptyStack())
{
cout << stack.top() << " ";
stack.pop();
}
else
stack.push(num + 3);
}
cin >> num;
}
cout << endl;
cout << "Stack Elements: ";
while (!stack.isEmptyStack()){
cout << " " << stack.top();
stack.pop();
}
cout << endl;
}
Explanation / Answer
Output:
34
62
Stack Elements: 63 15 39 86
HOW?:
The Elements in the stack is : 63 15 39 86
Input: Result 31 As there are no elements in stack the number not divisible by 2 or 3 so it will be inserted as 31 + 3 = 34 47 47 not divisible by 2 or 3 and stack is not empty so 34 element will be removed 86 divisible by 2 so inserted into stack 39 divisible by 3 so inserted into stack 62 divisible by 2 so inserted into stack 71 not divisible by 2 or 3 and stack is not empty so last insreted element 62 will be removed 15 divisible by 3 so inserted into stack 63 divisible by 3 so inserted into stackRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.