Question #29922

hi i am looking for help on this i think the answer is: 1, 3, 5. Any help would be great.

for(int i = 1; i < 10; i+=2)

{

cout<< i <<endl;

if( i % 5 == 0)

break;

}

for(int i = 1; i < 10; i+=2)

{

cout<< i <<endl;

if( i % 5 == 0)

break;

}

Expert's answer

Yes, you are right, answer is "1 3 5". Theinitial value of the i variable is 1. and each step it is incremented by 2, so it will

be (2n+1) on the n^{th} step. So loop will just print odd numbers: 1, 3, 5, 7, 9...But there is condition "if (i % 5 = 0)" that holds when i = 0, 5,

10, 15.... So the first value at which the condition is met will be 5, and an

indication "break" will stop the loop.

be (2n+1) on the n

10, 15.... So the first value at which the condition is met will be 5, and an

indication "break" will stop the loop.

## Comments

## Leave a comment