ข้อที่ 5.3
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
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();
}
}
}
ข้อที่7.7
using System;
namespace studentname
{
class student
{
private int idNumber;
private string lastName;
private double gradePointAversge;
public int getld()
{
return idNumber;
}
public string getName()
{
return lastName;
}
public double getGPA()
{
return gradePointAversge;
}
public void setLd(int id)
{
idNumber = id;
}
public void setName(string name)
{
lastName = name;
}
public void setGPA(double gpa)
{
gradePointAversge = gpa;
}
class Program
{
static void Main()
{
student st = new student();
st.setLd(9353);
st.setName("Teerawat");
st.setGPA(3.98);
Console.WriteLine("Student name {0} has ID # {1} and gpa of{2}",
st.getName(), st.getld(), st.getGPA());
Console.ReadLine();
}
}
}
}
ข้อที่ 8.6
using System;
namespace UserPredefineMethods
{
static class Program
{
static void Main()
{
double[] waterDepht = { 45, 19, 2, 16.8, 190, 0.8, 510, 6, 18 };
string outputMsg = "";
string caption = "System.Array Methods IIIustrated";
double[] w = new double[20];
outputMsg += "waterDepth Array\n\n";
foreach (double wVal in waterDepht)
outputMsg += wVal + "\n";
MessageBox.Show(outputMsg, caption);
Array.Copy(waterDepht, 2, w, 0, 5);
Array.Sort(w);
outputMsg = "Array w Sorted\n\n";
foreach (double wVal in w)
{
if (wVal > 0)
outputMsg += wVal + "\n";
}
MessageBox.Show(outputMsg, caption);
Array.Reverse(w);
outputMsg = "Array w Reversed\n\n";
foreach (double wVal in w)
{
if (wVal > 0)
outputMsg += wVal + "\n";
}
MessageBox.Show(outputMsg, caption);
}
}
}