+ 2

Hey friends ! please explain me this code below and say me why output is 1121231234

#include <iostream> using namespace std; int main() { for (int i=1;i<=4;i++) for(int j=1;j<=i;j++) cout<<j; return 0; } //output is 1121231234

24th Aug 2018, 12:16 PM
Albert Whittaker :Shaken To Code
Albert Whittaker :Shaken To Code - avatar
2 Antworten
+ 7
Akash Pal There's no slight error in the OP's code. Each for loop has one single statement in its scope and adding brace just make it more readable.
24th Aug 2018, 12:38 PM
Babak
Babak - avatar
+ 4
The first loop works first with initial value i = 1 and the loops works till I equal to 4. Inside that Another loop with initial value j = 1 and works till j less than or equal to i. First i = 1, the inner loop will be 1 to 1 printing value of j as 1. Then the value of i increases to 2, th inner loop works printing values from 1 to 2 and so on till I equal to 4. There is a slight error in your code. The correct code. #include <iostream> using namespace std; int main() { for (int i=1;i<=4;i++){ for(int j=1;j<=i;j++){ cout<<j; } } return 0; } //output is 1121231234
24th Aug 2018, 12:25 PM
Akash Pal
Akash Pal - avatar