# How to calculate Compound Interest in Java? Compound Interest Calculator in Java Example Tutorial

Hello guys, if you are wondering how to calculate compound interest in Java or looking for Java program to calculate compound interest then you have come at the right place. Earlier I have shared how to calculate simple interest in Java and today I am going to share how to calculate compound interest in Java program. 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:

## Compound Interest Calculator in Java - Example Program

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' ");

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'
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.

Other Coding Problems for Practice

• How to solve the FizzBuzz problem in Java? (solution)
• How to check if a given number is prime or not? (solution)
• Top 10 Programming problems from Java Interviews? (article)
• 7 Best Courses to learn Data Structure and Algorithms (best courses)
• Write code to implement the Quicksort algorithm in Java? (algorithm)
• 100+ Data Structure and Algorithms Problems (solved)
• Write a program to print the highest frequency word from a text file? (solution)
• 10 Books to learn Data Structure and Algorithms (books)
• How to remove duplicate elements from ArrayList in Java? (solution)
• How do you swap two integers without using a temporary variable? (solution)
• How to print the Pyramid pattern in Java? (solution)
• Write a program to check if a number is the power of two or not? (solution)
• How to find if given String is a palindrome in Java? (solution)
• 10 Free Courses to learn Data Structure and Algorithms (courses)
• How to find duplicate characters from String in Java? (solution)
• How do you reverse word of a sentence in Java? (solution)
• How to reverse an int variable in Java? (solution)
• How to find a missing number in a sorted array? (solution)
• How to check if a year is a leap year in Java? (answer)
• How to calculate factorial using recursion and iteration? (solution)
• How to reverse String in Java without using StringBuffer? (solution)
• Write code to implement the Bubble sort algorithm in Java? (code)
• Write a program to code insertion sort algorithm in Java (program)

If you like this coding problem and want to practice more to improve your coding skill, then you can also search for Java programming exercises in this blog.