How to calculate compound interest in Pascal

1 Answer

0 votes
program CompoundInterestCalculator;

uses
  Math; // Power

var
  Principal, Rate, Years, Amount, Interest: Real;

function CalculateCompoundInterest(P, R, T: Real): Real;
begin
  if (P < 0) or (R < 0) or (T < 0) then
  begin
    Writeln('Error: Principal, rate, and years must be non-negative values.');
    CalculateCompoundInterest := -1;
  end
  else
  begin
    // Calculate total amount using the compound interest formula
    Amount := P * Power(1 + R / 100, T);
    
    // Return compound interest
    CalculateCompoundInterest := Amount - P;
  end;
end;

begin
  Principal := 100000;
  Rate := 3.5;
  Years := 5;

  Interest := CalculateCompoundInterest(Principal, Rate, Years);

  if Interest >= 0 then
  begin
    Writeln('Principal Amount: ', Principal:0:2);
    Writeln('Annual Interest Rate: ', Rate:0:2, '%');
    Writeln('Years: ', Years:0:2);
    Writeln('Compound Interest: ', Interest:0:2);
    Writeln('Total Amount: ', (Principal + Interest):0:2);
  end;
end.




(*
run:

Principal Amount: 100000.00
Annual Interest Rate: 3.50%
Years: 5.00
Compound Interest: 18768.63
Total Amount: 118768.63

*)





answered Aug 30, 2025 by avibootz
edited Aug 30, 2025 by avibootz

Related questions

1 answer 74 views
1 answer 64 views
1 answer 80 views
1 answer 76 views
1 answer 72 views
...