public static double Calculate(CompoundInterestContext context)
        {
            double p = context.Principal;
            double r = context.InterestPercent / 100.0;
            int    n = context.CompoundingFrequency;
            int    t = context.InvestYears;

            double sum = p * Math.Pow(1 + r / n, n * t);

            return(Math.Round(sum, 2));
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.Write("What is the principal amount? ");
            string principalInput = Console.ReadLine();

            Console.Write("What is the rate: ");
            string rateInput = Console.ReadLine();

            Console.Write("What is the number of years: ");
            string yearsInput = Console.ReadLine();

            Console.WriteLine("What is the number of times the interest\n" +
                              "is compounded per years: ");
            string frequencyInput = Console.ReadLine();


            double principal;

            double.TryParse(principalInput, out principal);

            double interestPercent;

            double.TryParse(rateInput, out interestPercent);

            int years;

            int.TryParse(yearsInput, out years);

            int frequency;

            int.TryParse(frequencyInput, out frequency);


            var context = new CompoundInterestContext
            {
                Principal            = principal,
                InterestPercent      = interestPercent,
                InvestYears          = years,
                CompoundingFrequency = frequency,
            };

            double sum = CompoundInterest.Calculate(context);

            Console.WriteLine(
                $"${principal} invested at {interestPercent}% for {years} years");
            Console.WriteLine(
                $"compounded {frequency} times per year is ${sum}");
        }