Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

An interest calculation task I'm working on doesn't output the proper results

So I’m currently taking a C# fundamentals course as the entrance stage of a 6 month C# learning program. One of the tasks we have under the "Data Types and Variables" is an interest calculator.
The premise is such: You have a "deposit" that you write in the first line. The deposit has a 5% interest to it. The program needs to output the deposit’s value for 3 years, for each year, all at once.
So 100.00, 105.00, 110.25, 115.76. The numbers after the decimal point need to be rounded to 2 digits, as well as "The rules of math for rounding apply here".

double deposit = double.Parse(Console.ReadLine());
            double interest = (deposit*5)/100;

            double yearOne = deposit + interest;
            double yearTwo = yearOne + interest;
            double yearThree = yearTwo + interest;

            string totalSums = ($"{yearOne:N2}\n{yearTwo:N2}\n{yearThree:N2}\n");
            
            Console.WriteLine( totalSums );

This is the code I’ve written so far. It SEEMS to work, but it’s not as accepted.
If I put the deposit value as 100 (one of the examples), I get this output:
100
105.00
110.00
115.00

I’ve put the calculation for the interest percentage as double interest = (deposit*5)/100, which checks out if I use an external calculator to test it. But the program doesn’t give the right output.
Say if I put 100.23 as the deposit input, I’ll get: 105.24, 110.25, 115.26 when I should get 105.24, 110.50, 116.02. However, a round 100.00 doesn’t even display the digits after the decimal point, just rounds them down to the whole number.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I thought the problem comes from using double and not decimal due to floating-point precision issues, so I changed everything to decimal. I still get this exact problem. Nothing changes.

I’ve had other issues that I at least have ideas on where I’m going wrong, but this one has been screwing with me since 3 days. So I resorted to some help here.

Why am I not getting the correct outputs ? Where is my problem coming from ? Is it the math ? Or logic ? Maybe I’m not using the correct method ? I’m at a loss… And the learning resources I’ve been given don’t seem to help me with this issue, too. Like it’s the ONLY task that the resources don’t help with.

I’ve also seen other posts about compound interest, but they use arrays and other things that I’m not yet at the point of learning within the program. So code like that isn’t gonna pass the automatic tester. I’m not asking for a complete code solving or "cheat" if you will; I just need some guidance on what my issue here is, because I’m clueless at this point.

>Solution :

Your question is about compound interest, not simple interest. Therefore, you need to calculate the new interest every year.

double deposit = double.Parse(Console.ReadLine());

double yearOne = deposit + (deposit * 5)/100;
double yearTwo = yearOne + (yearOne * 5)/100;
double yearThree = yearTwo + (yearTwo * 5)/100;

string totalSums = ($"{yearOne:N2}\n{yearTwo:N2}\n{yearThree:N2}\n");
            
Console.WriteLine( totalSums );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading