-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Friday, May 15, 2020

Java Program To Calculate Compound Interest

  • May 15, 2020
  • by

Java Program To Calculate Compound Interest

In this Java Program, we calculate the Compound Interest.

The addition of interest to the principal sum of loan or deposit or in other words interest on interest is called as Compound Interest.

                                                P (1 + R/n) (nt) - P

Here, P = Principal Amount
      R = annual interest rate
      t = time the money is invested or borrowed for.
      n = number of times the interest is compounded per unit t


Let's say an amount of  $2,000 is deposited into a bank account as a fixed deposit at an annual interest rate of 8 %, compound monthly, the compound interest after 5 years would be:

P  = 2000
R = 8/100 = 0.08 (decimal)
n = 12
t =  5

Let's put these values into the formula

Compound Interest : 2000 (1 + 0.08 / 12)(12 * 5) - 2000 = $979.69

So, the compound interest after 5 years is $979.69

Logic : double amount = p * Math.pow(1 + (r / n), n * t);
             double cinterest = amount - p;

Step 1 : Create class CompoundInterest

Step 2 : Create method calculate and initialize variables p, t, r and n.

Step 3 : Apply the logic 

Step 4 : Call the object of the CompoundInterest Class in the main method. 

Program :

public class CompoundInterest 
    {
        public void calculate(int p, int t, double r, int n)
        {
            double amount = p * Math.pow(1 + (r / n), n * t);
            double cinterest = amount - p;
            System.out.println("Compound Interest after " + t + "years:" + cinterest);
            System.out.println("Amount after " + t + "years:" + amount);
        }
        
        public static void main (String[] args)
        {
            CompoundInterest obj = new CompoundInterest();
            obj.calculate(2000, 5, .08, 12);
        }
    
}

Output:

Compound Interest after 5 years : 979.6914
Amount after 5 years : 2972.69141660321

For execution of the same do visit my YouTube Channel:



A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages