Answer to Question #254302 in C++ for Fisiwe Shezi

Question #254302

The following code is supposed to display the positive even numbers less than 12. That is, it will output the numbers

2, 4, 6, 8 and 10. However, there is a logical error in the code. Explain what the output of the code below will be.

Then write a small program including the code below and make the necessary changes to fix the code so that it

displays what it is intended to display. Ensure that your program works correctly. Only submit the program, not the

output.

Hint: Use variable diagrams to trace the program to help you find the logical error.

int x = 1;

while (x != 12)

{

cout << x << endl;

x = x + 2;


1
Expert's answer
2021-10-23T08:06:18-0400

The output of this program of infinite sequence of odd numbers: 1 3 5 ...

To fix it one should initialized x to 2

#include <iostream>
using namespace std;

int main() 
{
    int x = 2;
    while (x != 12)
    {
        cout << x << endl;
    x = x + 2;
    }

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS