Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q1. Explain what is wrong with the following while loops, and then also rewrite

ID: 672926 • Letter: Q

Question

Q1.   Explain what is wrong with the following while loops, and then also rewrite the correct loop.

int counter = 1;

while (counter < 100)

{

   cout << counter << “ ”;

   counter--;

}

  

Q2.   What is the output of the following program:

#include <iostream>

using namespace std;

int main()

{

   double y = 0;            // y is set to 0

   while (y < 10)

   {

      y += 3;              // y is incremented by 3

   }                       

   cout << y << " ";     

   return 0;

}

Q3.   What is the output of the following code fragment?

int Sum = 0,

outerCount = 1;

while (outerCount <= 3)

{

     int innerCount = 1;

     while (innerCount <= outerCount)

     {

         Sum = Sum + innerCount;

         innerCount++;

     }

outerCount++;

}

cout << Sum << endl;

Q4.   After execution of the following code fragment, what value is printed for Length?

int Length = 5, Count = 4;

while (Count <= 6) {

if (Length >= 100)

Length = Length - 2;

else

Length = Count * Length;

Count++;

}

cout << "Length = " << Length << endl;

int counter = 1;

while (counter < 100)

{

   cout << counter << “ ”;

   counter--;

}

  

Explanation / Answer

Q1.
increment counter like

counter++;

Q2.
O/p is

12

Q3.
9

Q4.
600