+ 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
9 Answers
+ 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,.
+ 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
+ 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 ๐
+ 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)
0
NonStop CODING can you explain the code please?
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
- 1
Why