โปรแกรมที่ 5.3
using System;
namespace Calculatelnterst{
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 slnterest = Console.ReadLine();
decimal mlnterest = Convert.ToDecimal(slnterest);
if (mPrincipal < 0)
{
Console.WriteLine("Principal cannot be negative");
mPrincipal = 0;
}
decimal mlnterestPaid;
mlnterestPaid = mPrincipal * (mlnterest / 100);
decimal mTotal = mPrincipal + mlnterestPaid;
Console.WriteLine();
Console.WriteLine("Principal = " + mPrincipal);
Console.WriteLine("Interest = " + mlnterest + "%");
Console.WriteLine();
Console.WriteLine("lnterest paid = " + mlnterestPaid);
Console.WriteLine("Total = " + mTotal);
Console.WriteLine("Press Enter to terminate...");
Console.Read();
}
}
}
โปรแกรมที่ 5.13
โปรแกรมที่ 5.13
using System;
namespace CalculatelnterestTableMoreForgiving {
class Program {
static void Main(string[] args) {
int nMaximumlnterest = 50; //กำหนดค่าดอกเบี้ยสูงสุด
decimal mPrincipal;
while (true) //วนลูปปรับเงินต้นที่มีค่าเป็นบวก
{
Console.WriteLine("Enter principal : ");
string sPrincipal = Console.ReadLine();
mPrincipal = Convert.ToDecimal(sPrincipal);
if (mPrincipal >= 0)
{
break; //ถ้าหากมีค่าเป็นบวกออกมาจากวนลูป
}
Console.WriteLine("Principal cannot be negatine");
Console.WriteLine("Try agian");
Console.WriteLine();
}
decimal mlnterest;
while (true) //วนลูปปรับอัตราดอกเบี้ยที่มีค่าเป็นบวกและไม่เกินค่าสูงสุด
{
Console.Write("Enter interest");
string slnterest = Console.ReadLine();
mlnterest = Convert.ToDecimal(slnterest);
if (mlnterest > 0 && mlnterest <= nMaximumlnterest)
{
break; //ถ้าหากมีค่าเป็นบวกและน้อยกว่าค่าสูงสุดจะออกจากลูป
}
Console.WriteLine("lnterest cannot be negative" +
"or greater than " + nMaximumlnterest);
Console.WriteLine("Try again");
Console.WriteLine();
}
Console.Write("Enter number of year: ");
string sDuration = Console.ReadLine();
int nDuration = Convert.ToInt32(sDuration);
Console.WriteLine();
Console.WriteLine("Principal = " + mPrincipal);
Console.WriteLine("lnterest = " + mlnterest + "%");
Console.WriteLine("Duration = " + nDuration + "year");
Console.WriteLine();
int nYear = 1;
while (nYear <= nDuration)
{
decimal mlnterestPaid;
mlnterestPaid = mPrincipal * (mlnterest / 100);
mPrincipal = mPrincipal + mlnterestPaid;
mPrincipal = decimal.Round(mPrincipal, 2);
Console.WriteLine(nYear + " - " + mPrincipal);
nYear = nYear + 1;
}
Console.WriteLine("Prees Enter to terminate...");
Console.Read();
}
}
}