HOME

The FreePascal Project

Add something here ...

The Free Pascal — Open source compiler for Pascal and Object Pascal


~kc4zvw: ./pascal_triangle
Factorial program in FreePascal
Enter a Number : 5

The factorial of 5 = 120.

        *** PASCAL TRIANGLE ***

Enter a Number : 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
~kc4zvw:

  

Pascal Triangle

Add something here ...


program PascalTriangle;

const
   numRows = 5;
   Max_size = 20;

var
   fact   : Longint;
   n      : integer;
   number : integer;

procedure Draw_Triangle(num : integer);

var
   i, j : integer;
   n    : integer;
   A    : array[1..Max_size, 1..Max_size] of integer;

begin
   n := num;
   for i := 1 to n do begin
      for j := 1 to i do begin
         if (j = 1) or (i = j) then
            A[i,j] := 1
          else
             begin
                A[i,j] := A[i-1, j] + A[i-1, j-1];
             end;
      end;
   end;
   for i := 1 to n do begin
      {Gotoxy(41-i,i);}
      for j := 1 to i do
         Write(A[i,j], ' ');
      WriteLn
   end;
   { ReadLn; }
end;

function factorial(n : integer) : Longint;
begin
   if (n < 1) then
      factorial := 1
   else begin
      factorial := n * factorial(n - 1);
   end;
end;


(* # binomial theorem, n choose k *)

function binomial(n,k : integer) : integer;
begin
   if (n - k) <= 0 then
      binomial := 1;

   if k <= 0 then
      binomial := 1
   else
      binomial := Round(factorial(n) / (factorial(k) * factorial(n - k)));
end;

begin
   number := 12;
   { Clrscr;}
   Write('Factorial program in FreePascal');
   WriteLn;
   Write('Enter a Number : ');
   Readln(n);
   fact := factorial(n);
   WriteLn;
   WriteLn('The factorial of ', n:1, ' = ', fact, '.');
   WriteLn;
   WriteLn(^I, '*** PASCAL TRIANGLE ***');
   WriteLn;
   Write('Enter a Number : ');
   Readln(number);
   Draw_Triangle(number);
end.

{ End of File }
  

Build Notes

Add something here ...

Header 2

Add something here ...

Header 3

Add something here ...

Header 4

Add something here ...

Header 5

Add something here ...





Revised: Sunday, April 11, 2021 at 23:44:39 PM (EDT)