0

Can anyone explain the logic behind this code?

https://code.sololearn.com/c2pgq0TqdyjH/?ref=app How can then one right a code for- 54321 5432 543 54 5

31st Aug 2018, 6:30 PM
Mrinal Sood
Mrinal Sood - avatar
5 ответов
+ 3
The outer for loop starts with the value of your number of rows and goes down to 1. The inner loop is limited by the current value of the outer loop, which has the effect of limiting the row to smaller values on each run. Replacing cout<<j with cout<< n - j + 1 will give you the output above for n = 5
31st Aug 2018, 7:29 PM
Dan Walker
Dan Walker - avatar
+ 2
Instead of changing the cout, you can also change the loops like this: for (int i = 0; i < n; i++) { for (int j = n; j > i; j--) { cout << j; } cout << endl; }
31st Aug 2018, 7:37 PM
Anna
Anna - avatar
+ 2
in the inner loop we output j, which starts at 1 and goes up to i :)
31st Aug 2018, 7:53 PM
Dan Walker
Dan Walker - avatar
+ 1
Dan Walker but if 'i' has value of 'n' in the start and say n=5 , then shouldn't the column start with 5 rather than 1 ( you might feel this is a silly question-sorry- just a beginner)
31st Aug 2018, 7:46 PM
Mrinal Sood
Mrinal Sood - avatar
+ 1
Oh ok , got it Thanks a lot
31st Aug 2018, 8:05 PM
Mrinal Sood
Mrinal Sood - avatar