none
Compound interest program inside a loop to show end table problem!! RRS feed

  • Question

  • Hey guys I have to complete this program for a compound interest calculator and need to have it loop 5 times in order to show the user a table at the end with their balances. Ex. Year 1             $5060, Year 2                $6000 and so on but am stuck trying to code it using a for loop. Here's what I have so far. If you guys could help me out with this I would really appreciate it!!

                    Console.WriteLine("----------------------");
                    Console.WriteLine("|Compound Interest App|");
                    Console.WriteLine("----------------------");
                    Console.WriteLine("This program is used to calculate the future value
                    
                      for (int i = 1; i < years; i++)
                     {

                            double principal;//The principal amount before interest
                            double futurevalue;//The value after interest
                            double annualint;//The annual interest rate
                            double years = 0;//How many years interest is earned




                             Console.Write("Please enter investment amount: ");
                             principal = Convert.ToDouble(Console.ReadLine());

                            Console.Write("Please enter annual interest rate in percentage: ");
                            annualint = Convert.ToDouble(Console.ReadLine());
                            annualint /= 100;

                            Console.Write("Please enter the number of years : ");
                            years = Convert.ToDouble(Console.ReadLine());

                           futurevalue = principal * Math.Pow((1 + annualint),//math equation
                                                                                   (years));


                                       Console.WriteLine($"Year            Balance");
                                       Console.WriteLine($"{i}           {futurevalue}");
                }    


    • Edited by OhhBnertt Sunday, October 27, 2019 11:10 PM
    Sunday, October 27, 2019 11:08 PM

Answers

  • Greetings OhhBnertt.

    You need to ask for all the data first, then do the calculations inside a loop.

                            // Declare the data.
    
                            double principal;//The principal amount before interest
                            double futurevalue;//The value after interest
                            double annualint;//The annual interest rate
                            double years = 0;//How many years interest is earned
    
    
                            // Get the data from the user.
    
                             Console.Write("Please enter investment amount: ");
                             principal = Convert.ToDouble(Console.ReadLine());
    
                            Console.Write("Please enter annual interest rate in percentage: ");
                            annualint = Convert.ToDouble(Console.ReadLine());
                            annualint /= 100;
    
                            Console.Write("Please enter the number of years : ");
                            years = Convert.ToDouble(Console.ReadLine());
    
    
                            // Now loop through the years.
                            // 'for' loops normally start at zero.
                            for(int i = 0; i < years; i++)
                            {
                                // Do the calculation for one year and show the results here.
                            }

    • Marked as answer by OhhBnertt Wednesday, November 27, 2019 5:25 PM
    Sunday, October 27, 2019 11:44 PM

All replies

  • Greetings OhhBnertt.

    You need to ask for all the data first, then do the calculations inside a loop.

                            // Declare the data.
    
                            double principal;//The principal amount before interest
                            double futurevalue;//The value after interest
                            double annualint;//The annual interest rate
                            double years = 0;//How many years interest is earned
    
    
                            // Get the data from the user.
    
                             Console.Write("Please enter investment amount: ");
                             principal = Convert.ToDouble(Console.ReadLine());
    
                            Console.Write("Please enter annual interest rate in percentage: ");
                            annualint = Convert.ToDouble(Console.ReadLine());
                            annualint /= 100;
    
                            Console.Write("Please enter the number of years : ");
                            years = Convert.ToDouble(Console.ReadLine());
    
    
                            // Now loop through the years.
                            // 'for' loops normally start at zero.
                            for(int i = 0; i < years; i++)
                            {
                                // Do the calculation for one year and show the results here.
                            }

    • Marked as answer by OhhBnertt Wednesday, November 27, 2019 5:25 PM
    Sunday, October 27, 2019 11:44 PM
  • Ok that worked! It's just stating it as year 0, year 1, year 2, year 3, year 4 if I put in 5 years in the beginning where it should be starting at year 1 and go to year 5. Any tips on how to fix that? Thanks!
    • Edited by OhhBnertt Monday, October 28, 2019 12:54 AM
    Monday, October 28, 2019 12:47 AM
  • Um, no. I guess I wasn't making myself clear.

    When I wrote "Do the calculation for one year", I meant that each time through the loop it will do one year. So the first time through (when i equals 0) it will do the first year, the  next time (i = 1) it will do the second year, and so on.

    I can't write too much code for you, because we aren't supposed to do homework assignments, but here is a hint.

    Suppose I wanted to print a series of integers, each one double the next, up to the given number of years. I could do it like this.

    // Initialise the starting number;
    int number = 1;
    
    // A loop that doubles the number each time through and displays the result.
    for(int i = 0; i < years; i++)
    {
       // Display the number we are up to.
       Console.WriteLine(number.ToString());
    
       // Calculate the next number, ready for the next time through the loop.
       number = number * 2;
    }

    I hope that gives you the general idea.

    Monday, October 28, 2019 12:58 AM
  • Yes that's great. I have the general idea down. I've been struggling with loops for a while but this helps a lot. I appreciate it!
    Monday, October 28, 2019 1:03 AM
  • I'm a little astonished at this question.  If you have a loop where i runs from 0 through 4, and you want it to print out 1 through 5, is it not patently obvious that you need to print i+1?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, October 28, 2019 5:48 PM
  • OhhBnert,

    It's a very bad idea to change posts so completely in an edit. Editing should be for fixing typos and adding small points of clarification. If you make major changes, the thread no longer makes sense to someone who comes along later and reads the posts from the beginning. Also, I kept checking yesterday to see if you had asked any further questions, but the number of posts was unchanged so I assumed you hadn't.

    As for the answer, see Tim's reply.

    Monday, October 28, 2019 10:34 PM