Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

View RSS Feed

JD1's Personal Development Blog

Compound Interest Calculator

Rate this Entry
Compound interest can be calculated with the following formula.

A = Amount
P = Principal
R = Rate
n = Years


We can calculate the amount in Java as shown below. We currently know the principal (10,000) and the rate (1%). The years can change depending on how much information we want. In this application, we'll just use 20.

class Class1{
	public static void main(String args[]){
		double amount;
		double principal = 10000;
		double rate = .01;
		for(int year = 1; year <= 20; year++){
			amount = principal * Math.pow(1+ rate, year);
			System.out.println(year + "  " + amount);

We initialise our variables as per usual. We then construct a For Loop which will give us 20 iterations. Using our formula, we calculate the amount. We used the Math.pow function because it allows us to use indices. The first parameter is what you want in the parentheses and the second parameter is what you want the power to be. Quite a simple calculator.