program FloydsTriangle;
{$mode objfpc}{$H+}{$J-}
{
Floyd's Triangle:
Row 1: 1
Row 2: 2 3
Row 3: 4 5 6
...
This program:
- Reads the number of rows.
- Prints Floyd's Triangle using a helper function.
}
uses
SysUtils; { For IntToStr }
function GetNextNumber(var Counter: Integer): Integer;
begin
Inc(Counter); { Built-in increment }
Result := Counter;
end;
procedure PrintFloydTriangle(Rows: Integer);
var
i, j: Integer;
Current: Integer;
begin
Current := 0; { First number will be 1 }
for i := 1 to Rows do
begin
for j := 1 to i do
begin
Write(IntToStr(GetNextNumber(Current)) + ' ');
end;
WriteLn;
end;
end;
var
n: Integer;
begin
n := 7;
if n < 1 then
begin
WriteLn('Number of rows must be positive.');
Halt(1);
end;
WriteLn('Floyd''s Triangle with ', n, ' rows:');
PrintFloydTriangle(n);
end.
{
run:
Floyd's Triangle with 7 rows:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
}