Question

Hello, please EXPLAIN how you got your answer. Thank you! int x = 1 int j;...

Hello, please EXPLAIN how you got your answer. Thank you!

int x = 1

int j;

for (j=0; j <=2  ; j++)

x = x * j ;

cout << x << endl;

What is the output of the C++ code above?

Answer:

A) 0

B) 1

C) 2

D) 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int x = 1;  // x is initialized to 1
int j;
for (j=0; j <=2; j++)   // here this for loop iterates 3 times. for j=0, j=1,j=2
    x = x * j;
// let's see what happens with the above for loop
// j=0
// x = x*j = x*0 = 0
// j=1
// x = x*j = 0*1 = 0
// j=2
// x = x*j = 0*2 = 0
// so, after the loop, value of x is 0
cout << x << endl;  // prints 0

Output:  0
Add a comment
Know the answer?
Add Answer to:
Hello, please EXPLAIN how you got your answer. Thank you! int x = 1 int j;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT