for loop printing number and spaces

i am trying to print numbers like this

54321
5432
543
54
5
but its not printing the way i want
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include<iostream>
using namespace std;
int main()
{
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<i;j++)
		{
			cout<<" ";	
		}
		for(int k=5;k>0;k--)
		{
			
			cout<<k;
		}
		cout<<endl;
	}
}
Last edited on
Change the 0 on line 11 to an i.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
int main()
{
	for(int i=0;i<5;i++)
    {
	  for(int k=5;k>i;k--)
      {
		cout<<k;
      }
	  cout<<endl;
    }
}


to learn more C++ programming, you can also follow: https://www.alphacodingskills.com/cpp/cpp-tutorial.php
Last edited on
1
2
3
4
5
6
7
8
#include <iostream>
int
main()
{
    for (unsigned n=54321; n; n /= 10) {
	std::cout << n << '\n';
    }
}

I think the OP (from his code anyway, if not the unformatted description in his post) actually wanted a diagonally-increasing number of spaces in front of each line.
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
using namespace std;
int main()
{
   string front;
   for ( int n = 54321; n; n /= 10, front += ' ' ) cout << front << n << '\n';
}


54321
 5432
  543
   54
    5
Topic archived. No new replies allowed.