Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,656 questions

55,396 answers

573 users

How to print Floyd's triangle in Pascal

1 Answer

0 votes
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 

}

 



answered Jul 10 by avibootz
...