PHP Program to calculate the compound interest


March 13, 2022, Learn eTutorial
2338

What is meant by Compound Interest?

Simple Interest is mostly used in the baking sector to calculate the growth of money. The formula used to find the compound interest is

A = P(1 + r/n)nt

Where p stands for Principal Amount, t stands for Number of years, r stands for Rate of interest and n stands for the number of times the interest is compounded. For example, suppose

p = 5000

r = 8 => 8/100 => 0.08

t = 4

n = 1

then

A = 5000(1 + (0.08/1))4*1 = 6,802.44

How is compound interest calculated in PHP?

Now apply this logic in the PHP programming language. We accept the values for the principal amount, interest rate, time in years, and the number of times the interest is compounded. Then we calculate the interest by using the formula P(1 + r/n)nt.

In this program, we are using the direct method to calculate the Compound Interest which is using the formula. First, we have to accept the principal amount, interest rate, time in years, and the number of times the interest is compounded from the user and assign it to variables p, n, r, t and use the formula P(1 + r/n)nt and assign the result into variable ci. And print the value of the variable ci as the compound interest.

ALGORITHM

Step 1: Read the Principal amount into the variable p

Step 2: Read the Annual nominal interest rate as a percent into the variable R

Step 3: Read the time in decimal years into the variable t

Step 4: Read the number of times the interest is compounded into the variable n

Step 5: Assign the computed value of 'R / 100' into the variable r

Step 6: Assign the computed value of 'p * pow((1 + (r / n)), n * t)' into the variable ci

Step 7: Print the value of the variable ci as the compound interest

PHP Source Code

                                          <?php
$p = readline("Enter the Principal amount: ");
$R = readline("Enter the Annual nominal interest rate as a percent: ");
$t = readline("Enter the time in decimal years: ");
$n = readline("Enter the number of times the interest is compounded: ");
$r = $R / 100;
$ci =  $p * pow((1 + ($r / $n)), $n * $t);
echo "Compound Interest = $ci";
?>
                                      

OUTPUT

Enter the Principal amount: 10000
Enter the Annual nominal interest rate as a percent: 5
Enter the time in decimal years: 3
Enter the number of times the interest is compounded: 1
Compound Interest = 11576.25