BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Calculate Compound Interest

Last Updated: July 10, 2019 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to calculate compound interest.

Compound Interest Formula

Compound interest is calculated using the following formula:

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

Here P is principal amount.
R is the annual interest rate.
t is the time the money is invested or borrowed for.
n is the number of times that interest is compounded per unit t, for example if interest is compounded monthly and t is in years then the value of n would be 12. If interest is compounded quarterly and t is in years then the value of n would be 4.

Before writing the java program let’s take an example to calculate the compound interest.

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%, compounded 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 in the formula.

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

So, the compound interest after 5 years is $979.69.

Java Program to calculate Compound Interest

In this java program we are calculating the compound interest, we are taking the same example that we have seen above for the calculation.

public class JavaExample {

    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[]) {
    	JavaExample obj = new JavaExample();
    	obj.calculate(2000, 5, .08, 12);
    }
}

Output:

Compound Interest after 5 years: 979.6914166032102
Amount after 5 years: 2979.69141660321

Screenshot from Eclipse IDE:
Java Program to calculate compound interest

Related Java Examples

  1. Java program to calculate student Grades
  2. Java program to calculate average using array
  3. Java program to calculate area and circumference of Circle
  4. Java program to calculate area of triangle

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to Find Quotient and Remainder
  3. Java Program to Add Two Matrix using Multi-dimensional Arrays
  4. Java program to reverse a number using for, while and recursion
  5. Fascinating Number Program in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap