pyramid of numbers

For an assignment I am supposed to print out a pyramid that is a certain amount of columns wide and and the rows count. As an example, if the user enters the number 9 this would be the output:(note: I have # in place of the spaces at the moment)
####5
###654
##76543
#8765432
987654321

My code works fine for smaller numbers like this, but when I tested it with 13, this was the output:
######7
#####876
####98765
###10987654
##1109876543
#211098765432
32110987654321

however, the output for 13 should be this:
######7
#####876
####98765
###0987654
##109876543
#21098765432
3210987654321

I already am using % when printing the numbers and for the life of me I can't figure out how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>
using namespace std;

int main()
{
  int num;  //number of colums in the pyramid

  cout<< "Enter a positive odd number: ";
  cin>> num;

  while (num < 1 || num%2 !=1){
    cout<< "Entera positive odd number. Try again: ";
    cin>> num;
  }

  int row;     //used for counting rows in the loop
  int col;     //used for counting colums in the loop

  for (row=0; row<= (num/2); row++){
    for (col=(num/2)-row-1; col>=0; col--){
      cout<< "#";
    }
    for (col= (num/2)+row; col>= (num/2)-row; col--){
      cout<< (col%10)+1;
    }
    cout<< endl;
  }

  
  return(0);
}
Last edited on
Hi,

line 25 can be replaced with

cout<< ((col+1)%10);

HOPE IT HELPS
Topic archived. No new replies allowed.