Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
make a program with output like this (right align
)



6
56
456
3456
23456
123456

What I have tried:

#include<iostream>
using namespace std;
int main(){
	int i, j;
	int n = 6;
	
	for (i=1; i<=n; i++)
	{
		for (j=1; j<=i; j++)
		{
			cout<<i;
		}
		cout<<endl;
	}
	return 0;
}
Posted
Updated 22-Nov-21 22:02pm
Comments
Richard MacCutchan 23-Nov-21 3:52am    
The outer loop needs to go in reverse order from 6 down to 1. The inner loop needs to go from the current value of the outer loop index forwards to 6.

First of all you should learn to use a debugger
in order to see what your programme is doing.

If you are using Visual Studio you can follow this tutorial:
Tutorial: Debug C++ code - Visual Studio (Windows) | Microsoft Docs[^]

Your current output is
1
22
333
4444
55555
666666


Your outer loop counts from 1 to 6.

You must use an outer loop counting from 6 to 1 instead.
The inner loop must count from the current "i" to 6.

Have a look at this:

#include<iostream>
using namespace std;

int main(){
	int maximum = 6;
	
	for(int i=maximum; i>0; i--)
	{
	    for(int j=i; j<=maximum; j++)
	    {
	        cout << j;
	    }
	    
	    cout << endl;
	}
	
	return 0;
}
 
Share this answer
 
Comments
Firdaus Nandu 23-Nov-21 4:15am    
Thanks!
Maciej Los 23-Nov-21 5:03am    
5ed!
TheRealSteveJudge 23-Nov-21 5:19am    
Thank you!
In order to right align the numbers (without using cout facilities) you have just to precede your number with the right amount of blanks.
Moreover your current code simply duplicates i.
I give you a possible incipit:
C++
#include<iostream>
using namespace std;
int main()
{
  const int N = 6;

  for (int start=N; start>0; --start)
  {
    int items = (N-start+1);
    // TODO: here output '(N-items)' blanks, then output 'items' digits, then output a newline.
  }
  //...
 
Share this answer
 
v2
Comments
Maciej Los 23-Nov-21 5:03am    
5ed!
CPallini 23-Nov-21 5:05am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900