Detailed Print – CSKC
Sign In

Search

siteicon
Solved Problem!#CPP#3001 Back

Problem Statement - For to while conversion

Write the following program again using the while loop instead of for loop

		 

Click to open popup
  Related ( ? ) : 1995

Run Output

25 20 15 10 5

Solution

The program can be rewritten as follows.

#include <iostream>
using namespace std;
int main() 
{
  int i=50;         //The first instruction of for loop comes before while loop
  while(i>=10)      //The condition check remains here after while
  {
    cout<<i/2<<" "; //These are body instructions
    i-=10;          //The update instruction comes here as last instruction of loop body.
  } 
  return 0;
}