PHP Program to Calculate Simple Interest and Compound Interest

PHP

Simple Interest is the interest calculated only on the principal amount, while Compound Interest is calculated on both the principal and accumulated interest from previous periods. This article demonstrates how to calculate both types of interest using PHP.

Simple Interest Formula

The formula for calculating Simple Interest is

Simple Interest (SI) = P × R × T / 100

Where:

  • P is the principal amount
  • R is the interest rate per annum
  • T is the time period in years

Calculating Simple Interest Using Formula

Here's a direct implementation using the Simple Interest formula ?

<?php
function simpleInterest($principal, $rate, $time) {
    return ($principal * $rate * $time) / 100;
}

$principal = 15000;
$rate = 8;
$time = 3;

$simple_interest = simpleInterest($principal, $rate, $time);
echo "Principal: $" . number_format($principal) . "
"; echo "Rate: " . $rate . "% per annum
"; echo "Time: " . $time . " years
"; echo "Simple Interest: $" . number_format($simple_interest); ?>
Principal: $15,000
Rate: 8% per annum
Time: 3 years
Simple Interest: $3,600

Using Iterative Approach for Simple Interest

This method calculates interest for each year individually using a loop ?

<?php
function simpleInterestIterative($principal, $rate, $time) {
    $totalInterest = 0;
    for ($i = 0; $i < $time; $i++) {
        $totalInterest += ($principal * $rate) / 100;
    }
    return $totalInterest;
}

$principal = 5000;
$rate = 7;
$time = 4;

$total_interest = simpleInterestIterative($principal, $rate, $time);
echo "Simple Interest (Iterative): $" . number_format($total_interest);
?>
Simple Interest (Iterative): $1,400

Compound Interest Formula

The formula for calculating Compound Interest is

Compound Interest (CI) = P × (1 + R/100)^T - P

Where P, R, and T represent the same values as in Simple Interest.

Calculating Compound Interest Using Formula

Direct implementation using the Compound Interest formula with PHP's pow() function ?

<?php
function compoundInterest($principal, $rate, $time) {
    return $principal * pow((1 + $rate / 100), $time) - $principal;
}

$principal = 10000;
$rate = 10;
$time = 2;

$compound_interest = compoundInterest($principal, $rate, $time);
echo "Principal: $" . number_format($principal) . "
"; echo "Rate: " . $rate . "% per annum
"; echo "Time: " . $time . " years
"; echo "Compound Interest: $" . number_format($compound_interest, 2); ?>
Principal: $10,000
Rate: 10% per annum
Time: 2 years
Compound Interest: $2,100.00

Using Iterative Approach for Compound Interest

This method updates the principal amount each year to include accumulated interest ?

<?php
function compoundInterestIterative($principal, $rate, $time) {
    $amount = $principal;
    for ($i = 0; $i < $time; $i++) {
        $amount += ($amount * $rate) / 100;
    }
    return $amount - $principal;
}

$principal = 5000;
$rate = 8;
$time = 3;

$compound_interest = compoundInterestIterative($principal, $rate, $time);
echo "Compound Interest (Iterative): $" . number_format($compound_interest, 2);
?>
Compound Interest (Iterative): $1,299.20

Comparison

Method Time Complexity Use Case
Formula Approach O(1) Best for direct calculations
Iterative Approach O(n) Good for understanding step-by-step calculation

Conclusion

Both Simple and Compound Interest can be calculated efficiently in PHP using either direct formulas or iterative approaches. The formula method is more efficient with O(1) complexity, while the iterative method helps visualize year-by-year calculations.

Updated on: 2026-03-15T10:45:44+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements