Java Program to Calculate Compound Interest

Here is a Java Program to calculate Compound Interest.

public class CompoundInterest {

    public void calculate(int p, int t, int r, int n) {
        double amount = p * Math.pow(1 + (r / n), n * t);
        double interest = amount - p;
        System.out.println("Compond Interest is " + interest);
        System.out.println("Amount is " + amount);
    }
}

We take principal (p), time (t), rate of interest (r) and the number of times the interest is compound (n) as inputs. Amount is calculated using the formuale p * ( 1 + r/n )nt. In Java, the Math.pow() function is used to calculate powers. For example, to calculate 32, we use Math.pow(3,2). After finding amount, we find interest by subtracting principal.

Sample Execution :

Input :
p = 3400
t = 3
r = 4
n = 2

Output :
Compond Interest is 2475200.0
Amount is 2478600.0

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>