Question on C++ for loops 👇🏼👇🏼👇🏼 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Question on C++ for loops 👇🏼👇🏼👇🏼

What's the code for printing this format 👇🏼: 1 2 3 3 4 5 4 5 6 7 5 6 7 8 9

13th Mar 2022, 1:48 PM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
8 Answers
13th Mar 2022, 3:08 PM
NonStop CODING
NonStop CODING - avatar
+ 1
What have tried so far.. ? Your try is first.. Use 2 loops, first outer loop tells how many to print from 2 nd loop does the job, For I =1 to n For j=I, print j succesive numbers from j. jump to next line..... as simple as that,.
13th Mar 2022, 1:57 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 I tried this one for (int t = 1; j <= 5; t++) { for (int r = t; r <= 5; r++) { cout << r; } cout << endl; } But it gives me this 👇🏼 12345 2345 345 45 5
13th Mar 2022, 2:07 PM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
+ 1
Just modify condition r<=5 to r < t+t 1st condition had typo, that you are using j instead of t. add space if you want cout<<r<<' '; look good everything 👍
13th Mar 2022, 2:13 PM
Jayakrishna 🇮🇳
+ 1
Alvin✓ Look again // variable row is for tracking the row // variable col is for tracking the column // just look carefully each row is starting with the number equal to its row number. for example row 1 is starting with number 1. Row 2 is starting with number 2 and so on... // and also note that each row is printing numbers equal to its row number. for example... // row 1st is printing only 1 number // row 2nd is printing only 2 numbers (2, 3) // row 3rd is printing only 3 num (3, 4, 5) // so it means, each row is printing numbers upto one less than twice of row number or simply less that twice of (2*row) // twice of 1st row is 2 // so 1st row has number < 2 (upto 1) // sumilarly, twice of 2 row is 4 // so 2nd row has number < 4 (upto 3) // twice of 3rd row is 6 // so 3rd row has numbers < 6 (upto 5) // similarly twice of 5th row is 10 // so 5th row has numbers < 10 (upto 9)
14th Mar 2022, 3:53 AM
NonStop CODING
NonStop CODING - avatar
0
NonStop CODING can you explain the code please?
13th Mar 2022, 7:33 PM
𝑨𝒍𝒗𝒊𝒏
𝑨𝒍𝒗𝒊𝒏 - avatar
0
#include <iostream> using namespace std; class Add { public: int i,j; }; int main() { Add obj; for(obj.i=1; obj.i<=5; obj.i++) { for(obj.j=5; obj.j>=obj.i; obj.j--) { cout<<obj.j; } cout<<endl; } return 0; } Out Put this program 54321 5432 543 54 5 Like and. Follow
14th Mar 2022, 4:42 PM
Vinod Patidar
Vinod Patidar - avatar
- 1
Why
15th Mar 2022, 9:08 AM
Vinod Patidar
Vinod Patidar - avatar