## Compound Interest

The goal here is to teach you how to turn a complex equation into a single line of code:

This is the equation for compound interest: A = P(1 + r/n) Where A is the final amount after t years, r is the annual nominal interest rate, n is the number of times the interest is compounded per year, and P is the initial amount of money. Write a function which takes P,r,n, and t, returns the A value, but only contains a single line of code (braces and comments don’t count). If you aren't comfortable with functions or methods, assume you have those 4 variables and write one line of code to declare a variable called A, and assign it the right answer. |

If you have trouble with this question, try practising expression creation on our Code Calculator (C# only, though some Java will work because of syntax similarities).

*Try not to look at the answers until you’ve solved the problem*

## C#:## Show Code >>public static double compoundCalculator(double principleAmount, double rate, int compoundsPerYer, double years) { return principleAmount*Math.Pow((1+rate/compoundsPerYer),compoundsPerYer*years); } |

## Java:## Show Code >>public static double compoundCalculator(double principleAmount, double rate, int compoundsPerYer, double years) { return principleAmount*Math.pow((1+rate/compoundsPerYer),compoundsPerYer*years); } |

## Python:## Show Code >>def compoundCalculator(principleAmount, rate, compoundsPerYer, years): return principleAmount*pow((1+rate/compoundsPerYer),compoundsPerYer*years) |

Updated 25 November 2014 by Matthew F.