Interest table

This sample C# code shows how to calculate simple interest. The program interactively asks the user for Printiple amount, Interest rate and number of years and then calculates the interest.


public class CalculateInterestTable
{
public static void Main (string[] args)
{
Console.Write ("Enter principal amount: ");
decimal principal =
Convert.ToDecimal (Console.ReadLine());
if (principal < 0)
{ // principal cannot be negative
Console.WriteLine ("Principal cannot be negative");
principal = 0;
}

Console.Write ("Enter interest rate : ");
decimal interest = Convert.ToDecimal (Console.ReadLine());
if (interest < 0)
{ // interest cannot be negative
Console.WriteLine ("Interest cannot be negative");
interest = 0;
}

Console.Write ("Enter number of years : ");
int noYears = Convert.ToInt32 (Console.ReadLine());

Console.WriteLine ("\nPrincipal = " + principal
+ "\nInterest = " + interest + "%"
+ "\nDuration = " + noYears + " years\n");

// loop through number of years specified
int year = 1;
while (year <= noYears)
{
// calculate interest
decimal interestPaid = principal * (interest / 100);

// calculate new principal
principal += interestPaid;

// round to the nearest penny
principal = decimal.Round (principal, 2);

Console.WriteLine ("Year " + year + " $ " + principal);
year++;
}
Console.WriteLine ("\nPress Enter to stop");
Console.Read();
}
}


Comments

Author: TahiraSohaib31 Oct 2009 Member Level: Gold   Points : 0

this z goodone n helpfull



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: