Search code examples
c#principal

How to edit my method to where it correctly calculates simple interest taking into account the previous year's amount as the new principal?


Create an internal static void method that receives the original amount of investment principle as a named parameter with the default value 1,000.0, the amount of annual interest as a named parameter with the default value of .01 and the number of years duration of the investment as a named parameter with the default value of 5. The method uses format specifiers to output a simple table showing each year and the value of the investment at the end of each year. There is no input in this method. Tip: Look up the currency format specifier.

I am using A = P(1+rt) as the simple interest formula.

internal static void Table(double original, double interest, int[] yrs)
    {
        double[] A = new double[6];

        for (int i = 1; i < yrs.Length; i++)
        {
            A[i] = original * (1 + interest * i); //this is the line
        }

        for (int i = 1; i < yrs.Length; i++)
        {
            Console.WriteLine("Year " + i + " value: " + A[i].ToString("C", CultureInfo.CreateSpecificCulture("en-US")));
        }
    }

Solution

  • You forgot to add the default values for method parameters.
    and years should be integer not an integer array.

    internal static void Table(double original = 1000.0, double interest= 0.01, int years = 5)
    {
        for (int i = 1; i <= years; i++)
        {
            original += original * interest;
    
            Console.WriteLine($"Year {i} value: {original.ToString("C", CultureInfo.CreateSpecificCulture("en-US"))}");*
        }
    }