"We can only see a short distance ahead, but we can see plenty there that needs to be done." - Alan Turing
Simplest Solutions
  • Welcome
  • Code Katas
    • White Belt >
      • Hello World
      • Simple IO
      • Compound Interest
    • Yellow Belt >
      • Fibonacci Sequence
      • Perfect Numbers
      • Prime Checker
      • Prime Lister
      • Binary to Decimal
      • Decimal to Binary
      • Array Reversal
      • Dictionary of Rainfall-Data
    • Orange Belt >
      • Largest Prime Factor
      • Array Insertion
    • Green Belt
    • Blue Belt
    • Brown Belt
    • Black Belt
  • Freeware and Games
    • Poker Probability
    • Simple Sudoku
    • Code Calculator
    • Matrix Code
    • Keyword Permuter
  • Articles
    • Beginner Programming Lessons >
      • Value Vs. Reference Types
      • 2D Lists
      • Dictionaries
  • Blog
  • About
  • Contact

Compound Interest

Loading...

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)nt

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.

Services

Free Programs

Company

About us
Contact us

Support

Contact us
Get a $10 reward for publishing a FREE site with Weebly
Copyright  © 2017 | Simplest Solutions