Calculate Simple Interest and Compound Interest in PHP



What is Simple Interest and Compound Interest?

Simple Interest is the interest that depends only on the principal amount taken while compound interest is compounded, i.e., previous interest is added in the current time period. In this article, we are going to learn how to calculate simple interest and compound interest using PHP, as this PHP tutorial is important for beginners. 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.
  • T is the time-period in years.

Example 1

Input
principal = 15,000
rate = 8
years = 3
Output
Simple Interest = 3,600

Example 2

Input
principal = 5,000
rate = 7
years = 4
Output
Simple Interest = 1,400

Calculating Simple Interest Using Formula

First, we take input: the principal, the rate of interest, and the time period. Now we use the formula to calculate simple interest. After putting all values in the formula we return the simple interest. The formula for Simple Interest is:

Simple Interest = P × R × T / 100

Example

<?php
function simpleInterest($principal, $rate, $time) {
   return ($principal * $rate * $time) / 100;
}
$principal = 15000;
$rate = 8;
$time = 3;
$simple_Interest = simpleInterest($principal, $rate, $time);
echo "The Simple Interest is: " . $simple_Interest;
?>

Output

The Simple Interest is: 3,600

Time Complexity: O(1), constant time.

Using the Iterative Approach

In this approach, we use an iterative method to calculate simple interest. This method manually adds the interest for each year in a loop. After the loop completes, we return the total simple interest as the answer.

Example

<?php
function simpleInterestIterative($principal, $rate, $time) {
    $totalInterest = 0;
    for ($i = 0; $i < $time; $i++) {
        $totalInterest += ($principal * $rate) / 100;
    }
    return $totalInterest;
}
$principal = 15000;
$rate = 8;
$time = 3;
$total_Interest = simpleInterestIterative($principal, $rate, $time);
echo "The Simple Interest is: " . $total_Interest;
?>

Output

The Simple Interest is: 3600

Time Complexity: O(n), as we are using a loop to traverse for each year.

Calculating Compound Interest in PHP

Unlike simple interest, compound interest is calculated on the initial principal and also on the accumulated interest from previous periods. The formula to calculate Compound Interest is:

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

Where:

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

Example 1

Input
principal = 10,000
rate = 10
years = 2
Output
Compound Interest = 2100

Example 2

Input
principal = 5,000
rate = 8
years = 3
Output
Compound Interest = 1299.20

Using Formula

We first take the principal as P, the rate as R, and time period as T. Now we use the formula for calculating compound interest. After calculating, we return the result. The formula for Compound Interest is:

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

Example

<?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 "The Compound Interest is: " . $compound_Interest;

?>

Output

The Compound Interest is: 2100

Time Complexity: O(1), constant time.

Using the Iterative Approach

We can use an iterative approach to calculate compound interest. In this approach, instead of using a direct formula, we can calculate compound interest iteratively by updating the principal each year.

Example

<?php
function compoundInterestIterative($principal, $rate, $time) {
    $amount = $principal;
    for ($i = 0; $i < $time; $i++) {
        $amount += ($amount * $rate) / 100;
    }
    return $amount - $principal;
}
$principal = 10000;
$rate = 10;
$time = 2;
$compound_Interest = compoundInterestIterative($principal, $rate, $time);
echo "The Compound Interest is: " . $compound_Interest;

?>

Output

The Compound Interest is: 2100

Time Complexity: O(n), as we use a loop for each year.

Updated on: 2025-03-24T14:06:01+05:30

479 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements