using System;
namespace CalculateInterestTableMoreForgiving
{
class Program
{
static void Main (string[] args)
{
int nMaximumInterest = 50;
decimal mPrincipal;
while (true)
{
Console.Write("Enter principal");
string sPrincipal = Console.ReadLine();
mPrincipal = Convert.ToDecimal(sPrincipal);
if (mPrincipal >= 0)
{
break;
}
}
Console.WriteLine("Principal cannot be negatine");
Console.WriteLine("Try again");
Console.WriteLine();
decimal mInterest;
while (true)
{
Console.Write("Enter interest: ");
string sInterest = Console.ReadLine();
mInterest = Convert.ToDecimal(sInterest);
if (mInterest >= 0 && mInterest <= nMaximumInterest)
{
break;
}
Console.WriteLine("Interest cannot be negative " + "or greater than " + nMaximumInterest);
Console.WriteLine("Try again");
Console.WriteLine();
}
Console.Write("Enter number of years: ");
string sDuration = Console.ReadLine();
int nDurstion = Convert.ToInt32(sDuration);
Console.WriteLine();
Console.WriteLine("Principal =" + mPrincipal);
Console.WriteLine("Interest =" + mInterest + "%");
Console.WriteLine("Duration =" + nDurstion + "years");
Console.WriteLine();
int nyear = 1;
while (nyear <= nDurstion) ;
{
decimal mInterestPaid;
mInterestPaid = mPrincipal * (mInterest / 100);
mPrincipal = mPrincipal + mInterestPaid;
mPrincipal = decimal.Round(mPrincipal, 2);
Console.WriteLine(nyear + " - " + mPrincipal);
nyear = nyear + 1;
}
Console.WriteLine("Press Enter to terminate...");
Console.Read();
}
}
}
using System;
namespace CalculateInterest
{
class Program
{
static void Main ()
{
Console.Write("Enter principal: ");
string sPrincipal = Console.ReadLine();
decimal mPrincipal = Convert.ToDecimal(sPrincipal);
if (mPrincipal < 0)
{
Console.WriteLine("Principal cannot be negative");
mPrincipal = 0;
}
Console.Write("Enter interest: ");
string sInterest = Console.ReadLine();
decimal mInterest = Convert.ToDecimal(sInterest);
if (mInterest < 0)
{
Console.WriteLine("Interest cannot be negative");
mInterest = 0;
}
decimal mInterestPaid;
mInterestPaid = mPrincipal * (mInterest / 100);
decimal mTotal = mPrincipal + mInterestPaid;
Console.WriteLine();
Console.WriteLine("Principal =" + mPrincipal);
Console.WriteLine("Interest =" + mInterest + "%");
Console.WriteLine();
Console.WriteLine("Interest paid =" + mInterestPaid);
Console.WriteLine("Total =" + mTotal);
Console.WriteLine("Press Enter to terminnate...");
Console.ReadLine();
}
}
}
โปรแกรมที่ 5.13