## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

How to make pyramid Numbers & Stars in Pascal ? Posted by Blank_Voice on 30 Oct 2012 at 10:17 PM
Like this One..
Re: How to make pyramid Numbers & Stars in Pascal ? Posted by DWDuck on 31 Oct 2012 at 2:37 AM
Good day well what you need to do is run a for within a for... the First of which will obivious run the full length u need and the other only to the current position....

It should look like this...

Now before we start there is a few things we need to take into consideration...

eventually we want it tolook like
*
*1*
*121*
*12321*
*1234321*
*123454321*
*12345654321*
*1234567654321*
*123456787654321*
*****************
and everthing else must be centred ontop if it and that is the tricky part. This caused all the lines to be of odd lengths

Program pyramid;
Uses Crt;

Var
J,K,N,M : Integer;
{J will be our counter for each line}
{K will be our counter for each character per line}
{N will be out goal number of lines}
{M will be used to centre everything}
str1 : string;

Begin
Writeln('How many lines must the pyramid be: ');

Writeln;
M := (N);
Writeln('*':0:m); {This places our first star at the top in the middle}
For J := 1 to N-1 do {This is the pyramid}
Begin
Write('*':0:m-J); {creates the leading "*",also positioning us at the correct spot}
For K := 1 to J do {Fills the line with 123...}
Begin
Write(k);
End;
For K := J-1 downto 1 do {Completes the line with ...321, note becus the first line will be from 0 downto 1 nothing will happen}
Begin
Write(k);
End;

Write('*'); {Creates the trailing "*"}
Writeln; {Takes us to the next line}
End;
For K := 1 to N do {Makes the last line only stars the amount as entered at the start}
Begin
Write('*');
End;

Writeln;
Writeln('Press any key to exit program');
End;

Hope this help u out

Darkwing Duck aka DWduck signing off :)
Re: How to make pyramid Numbers & Stars in Pascal ? Posted by DWDuck on 31 Oct 2012 at 2:39 AM
Good day well what you need to do is run a for within a for... the First of which will obivious run the full length u need and the other only to the current position....

It should look like this...

Now before we start there is a few things we need to take into consideration...

eventually we want it tolook like
*
*1*
*121*
*12321*
*1234321*
*123454321*
*12345654321*
*1234567654321*
*123456787654321*
*****************
and everthing else must be centred ontop if it and that is the tricky part. This caused all the lines to be of odd lengths

Program pyramid;
Uses Crt;

Var
J,K,N,M : Integer;
{J will be our counter for each line}
{K will be our counter for each character per line}
{N will be out goal number of lines}
{M will be used to centre everything}
str1 : string;

Begin
Writeln('How many lines must the pyramid be: ');

Writeln;
M := (N);
Writeln('*':0:m); {This places our first star at the top in the middle}
For J := 1 to N-1 do {This is the pyramid}
Begin
Write('*':0:m-J); {creates the leading "*",also positioning us at the correct spot}
For K := 1 to J do {Fills the line with 123...}
Begin
Write(k);
End;
For K := J-1 downto 1 do {Completes the line with ...321, note becus the first line will be from 0 downto 1 nothing will happen}
Begin
Write(k);
End;

Write('*'); {Creates the trailing "*"}
Writeln; {Takes us to the next line}
End;
For K := 1 to N do {Makes the last line only stars the amount as entered at the start}
Begin
Write('*');
End;

Writeln;
Writeln('Press any key to exit program');
End;

Hope this help u out

Darkwing Duck aka DWduck signing off :)
Re: How to make pyramid Numbers & Stars in Pascal ? Posted by DWDuck on 31 Oct 2012 at 2:41 AM
Good day well what you need to do is run a for within a for... the First of which will obivious run the full length u need and the other only to the current position....

It should look like this...

Now before we start there is a few things we need to take into consideration...

eventually we want it tolook like
*
*1*
*121*
*12321*
*1234321*
*123454321*
*12345654321*
*1234567654321*
*123456787654321*
*****************
and everthing else must be centred ontop if it and that is the tricky part. This caused all the lines to be of odd lengths

Program pyramid;
Uses Crt;

Var
J,K,N,M : Integer;
{J will be our counter for each line}
{K will be our counter for each character per line}
{N will be out goal number of lines}
{M will be used to centre everything}
str1 : string;

Begin
Writeln('How many lines must the pyramid be: ');

Writeln;
M := (N);
Writeln('*':0:m); {This places our first star at the top in the middle}
For J := 1 to N-1 do {This is the pyramid}
Begin
Write('*':0:m-J); {creates the leading "*",also positioning us at the correct spot}
For K := 1 to J do {Fills the line with 123...}
Begin
Write(k);
End;
For K := J-1 downto 1 do {Completes the line with ...321, note becus the first line will be from 0 downto 1 nothing will happen}
Begin
Write(k);
End;

Write('*'); {Creates the trailing "*"}
Writeln; {Takes us to the next line}
End;
For K := 1 to N do {Makes the last line only stars the amount as entered at the start}
Begin
Write('*');
End;

Writeln;
Writeln('Press any key to exit program');
End;

Hope this help u out

Darkwing Duck aka DWduck signing off :)

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic