Write a nested loop for C++ to produce the following pattern if the user input is 5?

----1
---22
--333
-4444
55555

(-) accounts for number of spaces before each number

Recommended Answers

Show us what you have attempted.

Jump to Post

Hint you can also print multiple spaces using a for loop:

for (int i = 0; i < 10; i++ )
{
   cout << " ";

}
Jump to Post

All 5 Replies

Show us what you have attempted.

#include <iostream>

using namespace std;

int main()


    int input;
    cout << "Please enter an integer ";
    cin >> input;

    for (int i=1; i<=input; i++)
    {
        for (int j=1; j<=i; j++)
        {
        cout << i;
        ;
        }
        cout << endl;
    }

    return 0;
}


how to get the spaces by Setw() ? 

Hint you can also print multiple spaces using a for loop:

for (int i = 0; i < 10; i++ )
{
   cout << " ";

}

not working

Of course you won't see the spaces unless some character is at the end of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.