Patrekur Leví
Patrekur Leví

Reputation: 1

Bank interest calculator

I'm trying to make a calculator for interest in bank, like how many years would it take for 1$ to turn into 15$ with 4% interest, but all i get is the same number over and over, but i need it to go higher each year, like 1st year: 1$ * 4%interest, and 2nd year: 4% interest * 1st year interest, and 3rd year: 4% interest* 2nd year interest, and so on until it hits 15$

    private void btreikna_Click(object sender, RoutedEventArgs e)
    {
        double vextir4 = 0.04;
        double vextir8 = 0.08;
        double vextir12 = 0.12;
        double vextir16 = 0.16;
        double vextir20 = 0.2;

        double startvextir = Convert.ToDouble(byrjunisk.Text);
        double artal = Convert.ToDouble(tbartal.Text);


        double plusplus = vextir4 * startvextir;
        double count = artal;

        List<int> listfullofints = new List<int>();

        for (int i = 0; i < artal; i++)
        {
            int[i]utkoma = plusplus * artal;
        }

Upvotes: 0

Views: 1309

Answers (2)

Ian
Ian

Reputation: 30813

The classical formula for compound interest is:

V = (1 + r) ^ t

Where V is future value (or final number/original number), r is interest rate, and t is time.

Thus, in your case: V = 15 (from 15/1), r = 0.04, find t. Or, in other words:

t = log (V) / log (1 + r)

I recommend you to use Math.Log method.

double t = Math.Log(15D) / Math.Log(1.04D);

To get the time t you look for (without for loop). You may also be interested to look at the link JleruOHeP provides for interest calculation.

Upvotes: 1

JleruOHeP
JleruOHeP

Reputation: 10386

Your code is not very clear, but what you probably want is something like this:

decimal target = 15;
decimal start = 1;  
decimal interest = 0.04M;

decimal currentCapital = start;
var numOfYears = 0;
while (currentCapital < target)
{
    currentCapital = currentCapital + currentCapital*interest;
    numOfYears++;
}

Console.WriteLine(currentCapital + " in " + numOfYears);

Few notes about that code and your attempt. It is advised to use decimal for precise calculations (and you want to be precise with money :) ) In your code you are not updating your plusplus variable - it is always the very first interest. And the last note - you cant use for loop as you dont know ahead of time the number of executions.

Upvotes: 1

Related Questions