Spencer Wattamaniuk Spencer Wattamaniuk - 27 days ago 18
C# Question

Double Declining Balance In C#

So I need to create a method where I input years and amount and the console program will output a chart that shows the double-declining balance depreciation.

This is what I have.

static void doubleDecliningBalance(double amount, int years)
{

Console.WriteLine("{0,-20}{1,10}", "Year", "Depreciation");
Console.WriteLine("----------------------------------");
int count = 0;
double depreciation, amountLeft = 0;


while (count < years)
{
amountLeft = amount;
count = count + 1;
depreciation = (amountLeft * (2 / years));
amountLeft = amount - depreciation;
Console.WriteLine("{0,-20}{1,10:C2}", count, amountLeft);
}
}


Now when I run the program it won't depreciate. The formula is correct I think so I'm confused to why it won't depreciate, which means if I enter 5 years and a amount of $5000; it will just show 5000 for all 5 years.

Answer

You are subtracting from the original amount each iteration of the loop, which means that you do not depreciate from the current balance at each year, but from the starting balance.

If amount represents the original amount and amountLeft represents the current balance, try changing the loop into something like this

amountLeft = amount;
while (count < years)
{
    count = count + 1;                
    depreciation = (amountLeft * (2 / years));
    amountLeft -= depreciation;
    Console.WriteLine("{0,-20}{1,10:C2}", count, amountLeft);
}
Comments