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
*)