By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Compound Interest - C# Task

+2 votes

You really want a new TV, however you are a little short on money right now. You have a couple of options. You can get a loan from a bank or get a loan from a friend. Since you want to become the best programmer who ever lived, you decided to make a program to help you out.

Bank loans have interest rate and a term (number of years you have until you are required to pay the money back). Assume the bank loan will be for more than one year and the interest will be accrued on a yearly basis. Use this formula to calculate the amount of money you will have to pay back - 'FV = PV * (1 + i)n '. Where 'FV' (future value) is the money owed at the end of the period. 'PV' (present value) is the money you want to withdraw today, 'i' is the interest rate and 'n' is the term of the loan. Your friend is a really nice dude and he will loan you the money, however he wants a percentage of the money in return.

You will be given the price of the TV, the term and yearly interest rate for the loan from the bank, and the percentage your friend will ask for.

Your task is to write a program that calculates the best (cheapest) option to buy the TV. If the options are the same, choose your friend’s offer - you are a nice guy after all. Check the example to get a better understanding of the task.


The input data should be read from the console. It consists of four input values, each at a separate line:

  • The number pprice of the TV.
  • The number n – number of years you have until you must pay the bank back (term).
  • The number i – the yearly interest rate for the bank’s loan.
  • The number f – interest rate for your friend’s loan.

The input data will always be valid and in the format described. There is no need to check it explicitly.


  • The output data must be printed on the console.
  • On the only output line you must print the best loan price to the second digit after the decimal mark and the lender separated by a single space.


  • The number y is an integer in the range [0 ... 2 147 483 647].
  • The numbers p, i, f are floating-point numbers in the range [0 … 7.9 x 1028].
  • Allowed working time for your program: 0.25 seconds.
  • Allowed memory: 16 MB.


compaund interest task in C#

asked in C# category by user golearnweb
edited by user golearnweb

1 Answer

+1 vote
Best answer

The tricky part here is how to use the power-of - so you must use Math.Pow method, BUT - it can be used only with double: read here more (also see line #18)

So the solution is pretty interesting! :-)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class CompoundInterest
    static void Main()
        decimal p = decimal.Parse(Console.ReadLine());//The number p – price of the TV.
        double n = double.Parse(Console.ReadLine());//The number n – number of years you have until you must pay the bank back (term).
        double i = double.Parse(Console.ReadLine());//The number i – the yearly interest rate for the bank’s loan.
        decimal f = decimal.Parse(Console.ReadLine());//The number f – interest rate for your friend’s loan.

        double g = (1 + i);
        double h = Math.Pow(g, n);//Math.pow method is USED ONLY WITH double!!!
        decimal bank = p * (decimal)h;
        decimal friend = p * (1 + f);

        if (bank < friend)
            Console.WriteLine("{0:f2} Bank", bank);
        else if (friend <= bank)
            Console.WriteLine("{0:f2} Friend", friend);
answered by user hues
edited by user golearnweb
Thanks! It worked! :-)