Printing right-aligned triangle with descending numbers

Nov 17, 2015 at 9:05pm
I'm trying to figure out how to create output which looks like this.


   1
  21
 321
4321



This is the code I have right now. 'base' is user-defined.
1
2
3
4
int main()
{
   int base;
   cin >> base;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void displayPatternC(int base)
{
   cout << "\n***PATTERN C***\n";
   for(int height = 0; height < base; height++)
   {
      for(int count = base; count > height; count--)
      {
         cout << " ";
      }
      for(int count2 = 1; count2 <= height+1; count2++)
      {
         cout << count2;
      }
      cout << endl;
   }
}


Which results in:

    1
   12
  123
 1234
Last edited on Nov 17, 2015 at 9:29pm
Nov 17, 2015 at 9:48pm
Count2 in your second for loop is incrementing, and you're outputting it as it does so. You want this to decrement like your first count.
Nov 17, 2015 at 10:05pm
ah okay.

I rearranged some variables and got it to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void displayPatternC(int base)
{
   cout << "\n***PATTERN C***\n";
   for(int height = 0; height < base; height++)
   {
      for(int count = base; count > height; count--)
      {
         cout << " ";
      }
      for(int count2 = height; count2 >= 0; count2--)
      {
         cout << count2+1;
      }
      cout << endl;
   }
}


Thank you!
Topic archived. No new replies allowed.