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 terminate...");
Console.Read();
}
}
}
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
Console.WriteLine();
}
Console.Write("Enter number of years:");
string sDuration = Console.ReadLine();cannot be nagative" +
"or greater than " + nMaximumInterest);
Console.WriteLine("Try again");
int nDuration = Convert.ToInt32(sDuration);
Console.WriteLine();
Console.WriteLine("Principal = " + mPrincipal);
Console.WriteLine("Interest = " + minteresr + "%");
Console.WriteLine("Duration = " + nDuration + "years");
Console.WriteLine();
int nYear = 1;
while (nYear <= nDuration)
{
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 gradePointAverage;
public int getld()
{
return idNumber;
}
public string getName()
{
return lastname;
}
public double getGPA()
{
return gradePointAverage;
}
public void setld(int id)
{
idNumber = id;
}
public void setName(string name)
{
lastname = name;
}
public void setGPA(double gpa)
{
gradePointAverage = 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;
using System.Windows.Froms;
namespace UsePredefineMethods
{
static class Program
{
static void Main()
{
double[] waterDepth = { 45, 19, 2, 16.8, 190, 0.8, 510, 6, 18 };
string outputMsg = "";
string caption = "System.Array Methods lllustrated";
double[] w = new double[20];
outputMsg += "waterDepth Array\n\n";
foreach (double wVal in waterDepth)
outputMsg += "\n";
MessageBox.Show(outputMsg, caption);
Array.Copy(waterDepth, 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 Reverse\n\n";
foreach (double wVal in w)
{
if (wVal > 0)
outputMsg += wVal + "\n";
}
MessageBox.Show(outputMsg, caption);
}
}
}