Question 2a
Include the for loop below in a small program named question2a.cpp and complete the program. The loop should be executed 10 times. Do not change the for loop below. Compile and run your program to see for yourself that it works. You do not have to submit this program and output.
for (int i = 1; i <= n; i++)
if (i < 5 && i != 2)
cout << 'X';
Now convert the for loop into a while loop and add any variable initializations that you think are necessary. Name the program question2a.cpp, compile and run your program.
Question 2b
The following code is supposed to calculate the product of the numbers 2 through 5. That is, it should calculate the value of 2 x 3 x 4 x 5, which is 120. However, there is a logical error in the code. Explain what the output of the code below will be. Then write a small program named question2b.cpp, include 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.
Hint: Use variable diagrams to trace the program to help you find the logical error.
int next = 2, product = 1;
while (next <= 5)
{
next++;
product = product * next;
}
cout << "The product of 2 through 5 is " << product << endl;