Calculate the interest paid on a given principal over a period of years. - CSharp Language Basics

CSharp examples for Language Basics:decimal

Description

Calculate the interest paid on a given principal over a period of years.

Demo Code

using System;/* www.  ja  v a 2 s .  co  m*/
public class Program
{
   public static void Main(string[] args)
   {
      int maximumInterest = 50;
      Console.Write("Enter principal: ");
      string principalInput = Console.ReadLine();
      decimal principal = Convert.ToDecimal(principalInput);
      Console.Write("Enter interest: ");
      string interestInput = Console.ReadLine();
      decimal interest = Convert.ToDecimal(interestInput);
      Console.Write("Enter number of years: ");
      string durationInput = Console.ReadLine();
      int duration = Convert.ToInt32(durationInput);
      Console.WriteLine("Principal     = " + principal);
      Console.WriteLine("Interest      = " + interest + "%");
      Console.WriteLine("Duration      = " + duration + " years");
      int year = 1;
      while(year <= duration)
      {
         decimal interestPaid;
         interestPaid = principal * (interest / 100);
         principal = principal + interestPaid;
         principal = decimal.Round(principal, 2);
         Console.WriteLine(year + "-" + principal);
         year = year + 1;
      }
   }
}

Related Tutorials