Hey

I wanted to know how to make a number pyramid with (for example) the following formation:

(This would be for 5 rows, with 5 being a variable and as you can see in the last row the last number is 5 on each end)

So far I have this:

``````int j,i,n;
n=5; //Example
for (i=1;i<=n-1;i++)
{
printf ("%i",i);
for (j=i;j<n;j++)
{
printf ("/n");
}

}``````

But I seem to be stuck..........I have a feeling another loop is required but I just dont seem to know how to place it correctly.

Thanks for any help :)

Three loops.
Loop one -- prints a full line
Inside that loop is two loops:
Loop 1 prints the spaces before the '*'s
Loop 2 prints the '*'s

Three loops.
Loop one -- prints a full line
Inside that loop is two loops:
Loop 1 prints the spaces before the '*'s
Loop 2 prints the '*'s

*? Im guessing you mean that Loop 1 prints the number, Loop 1 inside of Loop 1 prints the /n, and Loop 2 inside of Loop 1 prints..............what?

First loop - say i - for each line so it runs from 0 to n-1
Inside this loop
{
First loop - j - goes from i to (2*i - 1) - print each j
Second loop (not nested in the j loop above) - j - goes from (2*i - 2) to i (backwards)
}

Once you get this working, add one more loop before the First loop j to add spaces.

First loop - say i - for each line so it runs from 0 to n-1
Inside this loop
{
First loop - j - goes from i to (2*i - 1) - print each j
Second loop (not nested in the j loop above) - j - goes from (2*i - 2) to i (backwards)
}

Once you get this working, add one more loop before the First loop j to add spaces.

Your instructions which result this code:

``````for (i=0;i<=n-1;i++)
{
for (j=i;j<2*i-1;j++)
{
printf ("%i",j);
}
for (j=2*i-2;j<i;j--)
{
printf ("%i",j);
}
}``````

Gives me a infinite loop just printing out numbers

Your instructions which result this code:

``````for (i=0;i<=n-1;i++)
{
for (j=i;j<2*i-1;j++)
{
printf ("%i",j);
}
for (j=2*i-2;j<i;j--)
{
printf ("%i",j);
}
}``````

Gives me a infinite loop just printing out numbers

The condition in the second internal for loop needs to be reversed to j>i

The condition in the second internal for loop needs to be reversed to j>i

Actually it should be j>=i

Also it would be a lot helpful if you start the first loop from 1 and go to n, to denote the starting number for each line, and thereby changing the condition of the first internal loop to j<=2*i-1.

Three loops.
Loop one -- prints a full line
Inside that loop is two loops:
Loop 1 prints the spaces before the '*'s
Loop 2 prints the '*'s

*? Im guessing you mean that Loop 1 prints the number, Loop 1 inside of Loop 1 prints the /n, and Loop 2 inside of Loop 1 prints..............what?

I am really curious? Is what you wrote actually what you think I wrote? Could you explain, please, how you translated my info? It makes no sense to me.

You probably want to write

``printf ("\n");``

``printf ("/n");``

:)

First loop - say i - for each line so it runs from 0 to n-1
Inside this loop
{
First loop - j - goes from i to (2*i - 1) - print each j
Second loop (not nested in the j loop above) - j - goes from (2*i - 2) to i (backwards)
}

I am really curious? Is what you wrote actually what you think I wrote? Could you explain, please, how you translated my info? It makes no sense to me.

``for (i=0;i<=n-1;i++)    {        for (j=i;j<2*i-1;j++)        {            printf ("%i",j);        }        for (j=2*i-2;j<i;j--)        {            printf ("%i",j);        }    }``

Exactly what I wrote..... :S

Word by word I translated what you wrote into code.

It appears to me that this will make an infinite loop.

``for (j=2*i-2;j<i;j--)``

You want it to go as long as j<i, but then decrement j each time? Shouldn't do that.

Also, why are you using the same variable (j) for both inner loops?

Something else I see, which may or may not be a problem. The order of operations causes multiplication to be done before subtraction, so I'd do this:

``for (j=i;j<2*(i-1);j++)``
``for (i=0;i<=n-1;i++)    {        for (j=i;j<2*i-1;j++)        {            printf ("%i",j);        }        for (j=2*i-2;j<i;j--)        {            printf ("%i",j);        }    }``

Exactly what I wrote..... :S

Word by word I translated what you wrote into code.

Hey you are matching my algorithm with someone else's comments :(

Actually it should be j>=i in the second loop's condition

Also it would be a lot helpful if you start the first loop from 1 and go to n (instead of 0 to n-1), to denote the starting number for each line, and thereby changing the condition of the first internal loop to j<=2*i-1.

Did you try the above?

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.