Traingular integer Sequence

How do i print the following using nested loops
654321
54321
4321
321
21
1
Flow Control Tutorial (including iteration statements like loops)
http://www.cplusplus.com/doc/tutorial/control/

What have you tried so far?
Last edited on
Hello imyourjoy,

You may also find https://www.learncpp.com/cpp-tutorial/57-for-statements/ useful.

Finished mine with the output of:

 6  5  4  3  2  1
 5  4  3  2  1
 4  3  2  1
 3  2  1
 2  1
 1


 Press Enter to continue:



Your turn.

Andy
my current progress is
654321
65432
654
65
6

I'll look into the websites that you guys have shared. Thanks alot!!
to loop in reverse, something like

for(int i = 6; i >0; i--)
cout << i << endl;

this is also one of the very small handful of places where a c style string can get a leg up.
char c[] = "654321";
char *cp = c;
..
cout << cp++; //in the loop. This makes it doable in 1 loop instead of nested with minimal effort.

once you know that, you can (ab)use c++ for it:
string c = "654321\0";
char *cp = &c[0];
as above.

gives you this shorty:
1
2
3
4
5
6
7
8
int main ()
{
string c = "654321\0";
char * cp = &c[0];
for(int i = 0; i < c.length(); i++)
  cout << cp++ << endl;
  return 0;
}
Last edited on
You don't need nested loops. Just one.

1
2
3
4
5
6
#include <iostream>

int main()
{
	for (const char* cp = "654321"; *cp; std::cout << cp++ << '\n');
}



654321
54321
4321
321
21
1

Topic archived. No new replies allowed.