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

All 5 Replies

Member Avatar for iamthwee

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() ? 
Member Avatar for iamthwee

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

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

}

not working

Member Avatar for iamthwee

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 meeting, networking, learning, and sharing knowledge.