Created
February 15, 2012 14:30
-
-
Save ChrisMoney/b64afb02b859e0fb0b1f to your computer and use it in GitHub Desktop.
C# --Calculates interest on a bank account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//C# Program = Calculate Interest Table with Functions | |
//CalculateInterestTableWithFunctions – generate an interest table much like the other interest table //programs, but this time using reasonable divison among several functions | |
Namespace CalculateInterestTableWithFunctions | |
{ | |
// Section 1 – input the data you will need to create the table | |
decimal mPrincpal = 0; | |
decimal mInterest = 0; | |
decimal mDuration = 0; | |
InputInterestData(ref mPrincipal, | |
ref mInterest, | |
ref mDuration); | |
//Section 2 – Verify the data by mirroring it back to the user | |
Console.WriteLIne(); //skip a line | |
Console.WriteLine(“Principal = “ + mPrincipal); | |
Console.WriteLine(“Interest + “%”); | |
Console.WriteLine(“Duration = “ + mDuration + “years”); | |
Console.WriteLine(); | |
//Section 3 – finally, output the interest table | |
OutputInterestTable(mPrincipal, mInterest, mDuration); | |
//wait for user to acknowledge the results | |
Console.WriteLine(“Press Enter to terminate…”); | |
} | |
//InputInterestData – retrieve from the keyboard the principal, interest and duration information //needed to create the future value table | |
//This function implements Section 1 by breaking it down into its three components | |
public static void InputInterestData(ref decimal mPrincipal, | |
ref decimal mInterest, | |
ref decimal mDuration, | |
{ | |
// 1a = retrieve the principal | |
mPrincipal = interestPositiveDecimal(“principal”); | |
//1b = now enter the interest rate | |
mInterest = InputPositiveDecimal(“interest”); | |
//1c = finally, the duration | |
mDuration = InputPostiveDecimal(“duration”); | |
} | |
//InputPositiveDecimal – return a positive decimal number from the keyboard. | |
//Inputting any one of principal, interest rate, or duration is just a matter of inputting a decimal number //and making sure that its positive | |
public static decimal InputPositiveDecimal (string sPrompt) | |
{ | |
// keep trying until the user gets it right | |
while(true) | |
{ | |
// prompt the user for input | |
Console.Write(“Enter” + “:”); | |
//Retrieve a decimal value from the keyboard | |
string sInput = Console.ReadLine(); | |
decimal mValue = Convert.ToDecimal(sInput); | |
// exit loop if the value entered is correct | |
if (mValue >= 0) | |
{ | |
// return the valid decimal value entered by the user | |
return mValue; | |
} | |
// otherwise, generate an error on incorrect input | |
Console.WriteLine(sPrompt + cannot be negative”); | |
Console.WriteLine(“Try again); | |
Console.WriteLine(); | |
}} | |
// Output interestTable – given the principal and interest generate a future value table for the number // of periods indicated in mDuration | |
public static void OutputInterestTable(decimal mPrincipal, decimal mInterest, decimal mDuration) | |
{ | |
for (int nYear = 1; nYear <= mDuration; nYear++) | |
{ | |
// calculate the value of the principal plus interest | |
decimal mInterestPaid; | |
mInterestPaid = mPrincipal * (mInterest / 100); | |
// now calculate the new Principal by adding the interest to the previous principal | |
mPrincipal = mPrincipal + mInterestPaid; | |
// round off the principal to the nearest cent | |
mPrincipal = decimal.Round(mPrincipal, 2); | |
// output the result | |
Console.WriteLine(nYear + “-“ + mPrincipal); | |
}}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment