Output writing – double increment in loop – CSKC
Sign In

Search

Solved Problem #CPP#2031    siteicon   siteicon  

Problem Statement - Output writing – double increment in loop

Write the output of the following code. Assume that required header files are included.

int main() 
{
  for(int i=10;i<=50;i+=10)
  cout<<i++/2<<" ";
  return 0;
}

Solution

TC++ #2031

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Notes

for(int i=10;i<=50;i+=10) cout<<i++/2<<” “;
This is a double increment loop. One increment is there in the for loop 3rd instruction and another inside the for loop. Watch –

First iteration : i will be 10, so i/2 will be 5;
Second iteration: i will become 10+1 and i+10 will become 21. now i/2 will be 10 as only integer portion of 21/2 will be retained.
Third iteration : i will be 21+2 because of i++ and i+10 will be 32 , so i/2 will be 16;
Forth iteration:i will be 32+1 which is 33, i+10 will be 43, so i/2 will be 21 because of integer division.
Fifth iteration attempt: i will be 43+1 which is 44 and i+10 would be 54, which is larger than 50 so loop will terminate.

So the output would be 5 10 16 21

 

  • Such programs need to be dry run by the user by putting values of loop variable one by one and then calculating the final value with patience.

Common Errors

  • It should be noted that third instruction which is i+=10 (increment by 10) is always tried as the last instruction of the loop and then only next condition of loop is tried.


Share

Created: 29-Dec-2017 19:59 | Updated: 22-Mar-2026 20:17|