# create a pyramid of numbers

Page 1 of 1

## 10 Replies - 50604 Views - Last Post: 16 August 2011 - 03:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=45259&amp;s=7f2835c7b4079a62f3e1a2855eb1c23d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 karlsimmons

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 28-February 08

# create a pyramid of numbers

Posted 05 March 2008 - 03:42 PM

I need to create a program that will create a pyramid of numbers (see attached file) in c++

What's the best way to do it? I know it has something to do with for-loops. Should I use nested for loops?

Any help is greatly appreciated.

#### Attached image(s)

This post has been edited by karlsimmons: 05 March 2008 - 05:59 PM

Is This A Good Question/Topic? 0

Reputation:

## Re: create a pyramid of numbers

Posted 05 March 2008 - 05:28 PM

```#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("Enter an integer\n");
int n,n1=1;
scanf("%d",&n);
clrscr();
for(short i=0;i<n;i+=1)
{
for(short j=0;j<=i;j+=1)
{
gotoxy(2*(i+1),(n-j));
printf("%d",n-i);
gotoxy(2*(2*n-i-1),(n-j));
printf("%d",n-i);
}
}
}

```

### #4 nekomata

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 24-October 09

## Re: create a pyramid of numbers

Posted 24 October 2009 - 02:37 AM

if i am correct, isnt the above code in C? What would that said code be like in c++?

### #5 stev3

• New D.I.C Head

Reputation: 1
• Posts: 44
• Joined: 04-June 09

## Re: create a pyramid of numbers

Posted 24 October 2009 - 10:58 AM

```#include <stdio.h>
int main()
{
int lines, i, j, k, tab = 0;
printf("Lines: "); scanf("%d",&lines);
k = lines;
for(i = 0; i < lines; i++) {
tab = 0;
do {
printf("   ");
tab++;
} while (tab < k);

for (j = 0; j < 1; j++) {
if(j < 10)
printf(" ");
printf("%d ", lines - k - j + 1);
}
for (j = 2; j < 2; j++) {
if(j < 10)
printf(" ");
printf("%d ", j);
}
printf("\n");
k--;
}
puts("\n:)");
return 0;
}

```

there has GOT to be a better way of doing that... (without conio)

This post has been edited by stev3: 24 October 2009 - 01:41 PM

### #6 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

## Re: create a pyramid of numbers

Posted 24 October 2009 - 10:59 AM

Looks like a pascals triangle to me... There are a ton of snippets on here for such programs. Search around and you will see, all the information is at the tip of your fingers. ^.^

### #7 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: create a pyramid of numbers

Posted 24 October 2009 - 11:14 AM

Crikies! First of all, this is not a pascal triangle. Pascal's triangle has sums of the two numbers above it. This is just playing with loops. Secondly, they asked for a C++ solution. Here is one you can toy around with...

```void equilateralTriangle(int rows) {
// Find the number of spaces for the row
int spaces = rows;

// Loop through the rows
for (int i = 0; i <= rows; i++) {

// Print the spaces in front of each row
for (int j = 0; j < spaces; j++) {
cout << "  ";
}

// Here we are printing the left side of each row (decrementing to 1)
for (int j = i; j >= 1; j--) {
cout << j << " ";
}

// Here we are printing the right side of each row (starting at 2 and incrementing)
for (int j = 2; j <= i; j++) {
cout << j << " ";
}

// Next row
cout << endl;

// Subtract the spaces
spaces--;
}
}

```

Just call the function with the number of rows you would like. So if you want to go from 6 to 1 to 6 you would just give it the number 6.

Play around and see what you can do with it. Enjoy!

"At DIC we be pyramid building code ninjas... think of us as Egyptian code builders. Don't tell him, but we hacked Ramses II's notebook."

This post has been edited by Martyr2: 24 October 2009 - 11:16 AM

### #8 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

## Re: create a pyramid of numbers

Posted 24 October 2009 - 01:00 PM

My bad on the bad info. I took a quick gander at it and THOUGHT it was Pascal's triangle, but was obviously mistaken... Whoopsy. Either way, slap me around a bit and call me sally, But there are plenty of code snippets jumpin out and about for things just as that.

### #9 nekomata

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 24-October 09

## Re: create a pyramid of numbers

Posted 25 October 2009 - 11:27 PM

oh thats rather interesting....step by step detail too. I'mma take this as a guide an go do it myself lol. <.> ty!

Reputation:

## Re: create a pyramid of numbers

Posted 13 March 2010 - 12:43 PM

nekomata, on 25 October 2009 - 10:27 PM, said:

oh thats rather interesting....step by step detail too. I'mma take this as a guide an go do it myself lol. <.> ty!

```#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,s;
clrscr();
for(i=1;i<=5;i++)
{
for(s=5;s>=i;s--)
printf(" ");
for(j=i;j>=1;j--)
{
printf("%d",j);
}
for(k=2;k<=i;k++)
{

printf("%d",k);
}
printf("\n");
}
getch();
}
```

### #11 Martyn.Rae

• The programming dinosaur

Reputation: 545
• Posts: 1,420
• Joined: 22-August 09

## Re: create a pyramid of numbers

Posted 13 March 2010 - 01:50 PM

I think this solution is cheating, but it works!

```#include <iostream>
using namespace std;

const char output[] = { "15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" };
const int values[15][2] = { { 34, 1 }, { 32, 5 }, { 30, 9 }, { 28, 13 }, { 26, 17 },
{ 24, 21 }, { 22, 25 }, { 20, 29 }, { 18, 33 }, { 15, 39 },
{ 12, 46 }, { 9, 52 }, { 6, 58 }, { 3, 64 }, { 0, 70 } };

int main()
{
int pyramid_size;   // The size of the pyramid
int pyramid_height; // The height of the pyramid
int pyramid_edge;   // The edge of the pyramid (indent)
int pyramid_blocks; // The pyramid blocks

while ( true ) {
cout << "Please enter a number in the range 1-15" << endl;
cin >> pyramid_size;
if ( pyramid_size < 1 || pyramid_size > 16 ) {
cout << "The value you entered was incorrect" << endl;
continue;
}
break;
}
cout << sizeof(output) << endl;
for ( pyramid_height = 0; pyramid_height < pyramid_size; pyramid_height++ ) {
for ( pyramid_edge = 0; pyramid_edge < values[pyramid_height][0]; pyramid_edge++ ) cout << " ";
for ( pyramid_blocks = 0; pyramid_blocks < values[pyramid_height][1]; pyramid_blocks++ )
cout << output[values[pyramid_height][0]+pyramid_blocks];
cout << endl;
}
return 0;
}

```

### #12 lenmorvash

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 16-August 11

## Re: create a pyramid of numbers

Posted 16 August 2011 - 03:51 PM

Stepler, on 05 March 2008 - 05:28 PM, said:

```#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("Enter an integer\n");
int n,n1=1;
scanf("%d",&n);
clrscr();
for(short i=0;i<n;i+=1)
{
for(short j=0;j<=i;j+=1)
{
gotoxy(2*(i+1),(n-j));
printf("%d",n-i);
gotoxy(2*(2*n-i-1),(n-j));
printf("%d",n-i);
}
}
}

```

hey thank you very much, this has been a lot of help to us
more power