PHP Program to Find Compound Interest Last Updated : 27 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Compound interest is a financial concept that calculates the interest on the initial principal and also on the accumulated interest of previous periods. It is widely used in the banking, finance, and investment sectors. In this article, we will explore different approaches to calculating compound interest using PHP. Table of Content Formula for Compound InterestCompound Interest using the Formula DirectlyCompound Interest using a LoopFormula for Compound InterestThe formula for calculating compound interest is: A = P (1 + \frac{r}{n})^{nt}Where A is the amount after t years.P is the principal amount (initial investment).r is the annual interest rate (in decimal).n is the number of times interest is compounded per year.t is the number of years.Compound Interest using the Formula DirectlyThe basic method is to calculate compound interest in PHP is by directly using the formula. In this example, the compoundInterest() function takes the principal amount, annual interest rate, time in years, and the compounding frequency as arguments. It returns the compound interest calculated using the formula. PHP <?php function compoundInterest($principal, $r, $t, $n) { $amount = $principal * pow((1 + $r / $n), $n * $t); $compoundInterest = $amount - $principal; return $compoundInterest; } // Driver code $P = 10000; $r = 0.05; $t = 5; $n = 12; echo number_format(compoundInterest($P, $r, $t, $n), 2); ?> Output2,833.59Compound Interest using a LoopAnother approach is to use a loop to calculate the compound interest. This method is useful when you need to track the interest accrued each compounding period. In this approach, the compoundInterest() function iterates over the total number of compounding periods and accumulates the interest in each period. This method can be more flexible when dealing with complex compounding scenarios. PHP <?php function compoundInterest($principal, $r, $t, $n) { $amount = $principal; $compoundPeriod = $t * $n; for ($i = 0; $i < $compoundPeriod; $i++) { $amount += ($amount * $r) / $n; } $compoundInterest = $amount - $principal; return $compoundInterest; } // Driver code $P = 10000; $r = 0.05; $t = 5; $n = 12; echo number_format(compoundInterest($P, $r, $t, $n), 2); ?> Output2,833.59 Comment More infoAdvertise with us Next Article PHP Program to Find Compound Interest V vkash8574 Follow Improve Article Tags : PHP PHP-math Similar Reads Compound Interest Compound Interest is the interest that is calculated against a loan or deposit amount in which interest is calculated for the principal as well as the previous interest earned. Compound interest is used in the banking and finance sectors and is also useful in other sectors. A few of its uses are:Gro 9 min read Monthly Compound Interest Formula The monthly compound interest formula is used to compute compound interest every month. Compound interest is sometimes known as interest on interest. The first period of compound interest resembles the first period of simple interest, but the second period is distinct. Interest is computed on the in 8 min read Monthly Compound Interest Formula Interest is the extra amount of fees paid to borrow money or any other assets. Interest is paid on a monthly, quarterly, or annual basis. The interest earned depends on the time for which money is invested, the amount of money invested, and the rate at which money is invested.Two common types of int 5 min read Quarterly Compound Interest Formula Interest is the additional money we pay for the use of some other person's money. When we borrow some amount of money from a person or organization we give them additional money as an incentive for it, this additional sum of money is called Interest. The amount of money you initially lend is called 4 min read Compound Interest | Class 8 Maths Compound Interest: Compounding is a process of re-investing the earnings in your principal to get an exponential return as the next growth is on a bigger principal, following this process of adding earnings to the principal. In this passage of time, the principal will grow exponentially and produce 9 min read Like