Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

How to Calculate Compound Interest in Java? Example Tutorial

Calculating simple interest or compound interest are common coding problem which are taught in schools and colleges to teach you coding. The best thing about these coding exercise in Java are that everybody are familiar with them so you know the concept already and all you need to do is convert that solution into code which is the skill needed to become a coder or programmer. If you remember your primary Maths classes then you would know that formula to calculate compound interest depends upon three things principle, rate, number of times interest is compounded per year, and time. This means you need to write a function which will accept these three data as parameters or method arguments. 

Now the next challenge is to decide the data type of each of these parameters. For the sake of simplicity you can consider time in year (full year),  rate in floating point like 10.5 or 7.5 and principle as int value, I mean maximum 2^32 - 1.  In real world this can be long to represent big numbers and time can also be in days. 

compounding time can be int also as quarterly compound means it would be 4 as interest is calculated and compounded every  months. If its half-yearly then it can be two as interest is computed twice after six months in an year.

Once this is sorted, the next challenge is to decide the return type of method. Since its going to return a compound interest which can contain decimals, it should be float or double in Java. Now, all of these are sorted and we can proceed with implementation.

One more detail we need is the exact formula of compound interest which is following:


How to Calculate Compound Interest in Java? Example Tutorial




Compound Interest Calculator Program in Java

Here is our complete Java program to calculate compound interest for given amount. You can play with this program in your favorite IDE like IntelliIDEA or Eclipse to learn more about how it works. There is no better way for learning then playing and making changes and eventually writing on your own.

import java.util.Scanner;

/*
* Java Program to calculate compound interest
*/
public class Main {

public static void main(String args[]) {

//creating scanner to accept radius of circle
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome in Java program to calculate compound interest");
System.out.println("Formula for calculating compound interest is 'p*( 1 + r/n ) ^nt' ");

System.out.println("Please enter principle :");
int principle = scanner.nextInt();

System.out.println("Please enter number of years :");
int time = scanner.nextInt();

System.out.println("Please enter interest rate (per annum):");
float rate = scanner.nextFloat();


System.out.println("Please enter number of times interest compounds :");
int n = scanner.nextInt();


double amount = compoundInterest(principle, time, rate, n );

System.out.println("Compound interest calculated by Java program is : " + amount);

scanner.close();
}

/**
* Java method to calculate compound interest. This uses formula
* 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.
* @param p is principle
* @param t is time
* @param r is rate
* @param n is number of times the interest is compounded
* 
*/
public static double compoundInterest(int p, int t, float r, int n) {
double amount = p * Math.pow(1 + (r / (100*n)), n * t);
double interest = amount - p;
return interest;
}
}


Output
Welcome in Java program to calculate compound interest
Formula for calculating compound interest is 'p*( 1 + r/n ) ^nt' 
Please enter principle :
1500
Please enter number of years :
6
Please enter interest rate (per annum):
4
Please enter number of times interest compounds :
4
Compound interest calculated by Java program is : 404.60154118524906


That's all about how to write a Java Program to calculate compound interest. The key thing we learned from this program is choosing right data type e.g. choosing int for interest rate can make the whole calculate wrong because Java would have used int arithmetic instead of floating point arithmetic. Another thing you learn is how to use the Math.pow() method to calculate power and how to convert a mathematical formal like we had for calculating compound interest into a Java program. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.