Can anyone explain the logic behind this code? | Sololearn: Learn to code for FREE!

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

8/31/2018 6:30:52 PM

Mrinal Sood

5 Answers

New Answer

+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

+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; }

+2

in the inner loop we output j, which starts at 1 and goes up to i :)

+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)

+1

Oh ok , got it Thanks a lot