A task from a challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

A task from a challenge

Hi everyone. Can anybody explain me how does the following code work, because i don't understand the result of it. #include <iostream> using namespace std; int main(){ for(int i=0;i<3;i++) for(int j=0;j<=i;j++) cout<<i<<endl; return 0; } // Output is 011222, shouldn`t it be 012, because we ouput only i and how it becomes 011222?

6th Jun 2017, 10:56 AM
Robert Wallace
Robert Wallace - avatar
1 Answer
+ 10
i = 0, j <= 0 means 0 will be printed once (for j = 0). i = 1, j <= 1 means 1 will be printed twice (for j = 0, 1). i = 2, j <= 2 means 2 will be printed thrice (for j = 0, 1, 2).
6th Jun 2017, 10:46 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar