Calculating compound interest

joel2usha

New member
The compound interest formula is A = P(1 + (r/n))^(nt).I have written a code in php and the output is 788.55 whereas my output in my Java program is 557.65. I am not sure why the values are different. Here is my java and my php code:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/

package com.mycompany.compoundinterest;

/**
*
* @author Joelm
*/
import java.lang.Math;
import java.util.Scanner;
public class CompoundInterest {

public static void main(String[] args) {

double principal;
int timePeriod;
double rate;
int numberofTimes;

double compoundInterest;

Scanner key = new Scanner(System.in);

System.out.print("Enter the principal amount: ");
principal = key.nextDouble();

System.out.print("Enter the interest rate: ");
rate = key.nextDouble();

System.out.print("Enter the number of times the interest is applied per time period: ");
numberofTimes = key.nextInt();

System.out.print("Enter the number of time periods elapsed: ");
timePeriod = key.nextInt();

compoundInterest = principal * (Math.pow(1 + (rate/numberofTimes), (numberofTimes * timePeriod)));

System.out.printf(" %.2f", compoundInterest);




}
}


<!DOCTYPE html>
<html>
<head>
<title>Compound Interest Calculator</title>
</head>
<body>
<h2>Compound Interest Calculator</h2>
<form method="post" action="">
<label for="principal">Principal Amount (P):</label>
<input type="decimal" name="principal" required><br><br>
<label for="rate">Annual Interest Rate (r):</label>
<input type="decimal" name="rate" required><br><br>
<label for="time">Time Period (t in years):</label>
<input type="text" name="time" required><br><br>
<label for="compounds">Number of Times Compounded Annually (n):</label>
<input type="number" name="compounds" required><br><br>
<input type="submit" name="calculate" value="Calculate">
</form>
<?php
if (isset($_POST['calculate'])) {
$principal = ($_POST['principal']);
$rate = ($_POST['rate']); // Convert percentage to decimal
$time = ($_POST['time']);
$compounds = ($_POST['compounds']);
$amount = $principal * (pow(1 + ($rate / $compounds), ($compounds * $time)));
echo "<h2>Result:</h2>";
echo "Principal Amount: $principal<br>";
echo "Interest Rate: $rate<br>";
echo "Compounds: $compounds<br>";
echo "Time Period: $time years<br>";
echo "Compound Interest: $amount";
}
?>
</body>
</html>
 
Check the updated PHP code, please:

Code:
<?php
if (isset($_POST['calculate']))
{
$principal = ($_POST['principal']);
$rate = ($_POST['rate'] / 100); // Convert percentage to decimal
$time = ($_POST['time']);
$compounds = ($_POST['compounds']);
$amount = $principal * pow(1 + ($rate / $compounds), $compounds * $time);

echo "<h2>Result:</h2>";
echo "Principal Amount: $principal<br>";
echo "Interest Rate: " . ($_POST['rate']) . '%<br>'; // Display the interest rate as a percentage
echo "Compounds: $compounds<br>";
echo "Time Period: $time years<br>";
echo "Compound Interest: $amount";
}
?>

I'm hoping it works out for you.
 
Back
Top