Given the following program segment….
for (num = 10; num >= 1; num--)
cout << setw(3) << num;
Write a while loop that will have the same output.
C++
Code:
int num = 10; //Initialization of loop variable
while(num >= 1) //Condition of loop
{
cout << setw(3) << num; // loop
statement
num--; //Decrement or Increment
}
Explanation: In for loop there is Initialization, Condition and Increment or Decrement. Convert for loop into while loop we need to take care of Initialization, Condition and Increment or Decrement.
Screenshot:
Get Answers For Free
Most questions answered within 1 hours.