0
votes

Please Help me out to print this pattern and also explain logic behind it. THank you in advance.

      0
    111
  22222
3333333

My Pascal Code is:

program patnum;
            uses crt;
            var
               row, col1, col2, totalRows, totalCol: integer;
            Begin
                 clrscr;
                 totalRows := 3;
                 totalCol := totalRows-2;
                        for row := 0 to totalRows do
                        begin
                                for col1 := 0 to totalCol do
                                begin
                                   write(' ');
                                end;
                                totalCol := totalCol - 1;  
                                for col2 := 0 to row do
                                begin
                                   write(row);
                                end;
                        writeln('');
                        end;
                 readln;
            End.

* Please Help *

2
Can you suggest me how do I come to possess good coding skills? - Riaz Ahmed
You can improve your coding skills by reading tutorials like tutorialspoint.com/pascal and by programming easy logic yourself. If you really want to be a good coder you have to play around with code, experiment and search some new tasks which you can solve. - Alexander Baltasar

2 Answers

0
votes

im sure there is a much more easy way to do this but if you just want to get it done with a good effort this should help.

 var
    t : integer       
 begin
    t := 6
    for x := 0 to t do
    begin
       write(" ");
    end;
    write("0");
    writeln;
    t := 4;
    for x := 0 to t do
    begin
       write(" ");
    end;
    write("111");

    writeln;
    t := 2;
    for x := 0 to t do
    begin
       write(" ");
    end;
    write("22222");
    writeln;
    t := 0;
    for x := 0 to t do
    begin
       write(" ");
    end;
    write("3333333");
 end.
-1
votes

This really seems a programming exercise. However, here it is:

Program patnum(output);

function digitNumber(n:integer) : integer;
var counter : Integer;
begin
    counter := 0;
    while n >= 1 do
    begin
        counter := counter + 1;
        n := n div 10;
    end;

    digitNumber := counter; 
end;

procedure prettyPrint(x1 : integer; x2 : integer; x3 : integer; x4 : integer);
var Counter, max, tmp : Integer; 
Begin

    max := digitNumber(x1);

    tmp := digitNumber(x2);
    if(tmp > max) then
    begin
        max := tmp;
    end;

    tmp := digitNumber(x3);
    if(tmp > max) then
    begin
        max := tmp;
    end;    

    tmp := digitNumber(x4);
    if(tmp > max) then
    begin
        max := tmp;
    end;


    tmp := max - digitNumber(x1);
    For Counter := 1 to tmp do
    Begin 
        write(' ');
    End; 
    writeln(x1);

    tmp := max - digitNumber(x2);
    For Counter := 1 to tmp do
    Begin 
        write(' ');
    End; 
    writeln(x2);

    tmp := max - digitNumber(x3);
    For Counter := 1 to tmp do
    Begin 
        write(' ');
    End; 
    writeln(x3);

    tmp := max - digitNumber(x4);
    For Counter := 1 to tmp do
    Begin 
        write(' ');
    End; 
    writeln(x4);
End;



begin
  prettyPrint(22,3333,1,534);
end.

I would suggest using an array to hold the values to be printed but I leave that to you...

Also, your code seems almost useless.

*edit: Note that the function fails on integers < 1.