Can someone tell me why only even numbers are printed for the piece of code below??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can someone tell me why only even numbers are printed for the piece of code below???

#include <iostream> using namespace std; int main() { int i=1; int sum=0; while(i++<=10){ cout<<i<<" "; sum+=i; i++; } cout<<"and the sum is:::"<<sum; return 0; }

17th Jul 2017, 2:36 PM
RiGeL
RiGeL - avatar
2 Answers
+ 6
notice i++ happening twice. that makes the odds get skipped.
17th Jul 2017, 2:52 PM
Millo
Millo - avatar
+ 5
because you are incrementing value of i twice
17th Jul 2017, 2:43 PM
Ashwaghosh Sadanshiv
Ashwaghosh Sadanshiv - avatar