// calculate interest paid annually on a given principle over // a period of specified years and show a table of the results // this is a console program // compiled with C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe using System; namespace CalcInterestTable { using System; public class InterestTable { public static void Main() { // define a maximum interest rate int maxInterest = 100; Console.Write("Program will ask you for the starting amount\n"); Console.Write("you deposit, the interest paid and the number\n"); Console.Write("of years you want the investment to last ...\n\n"); // keep prompting until you get the proper value decimal principal; while(true) { Console.Write("Enter starting principal amount: "); string sPrincipal = Console.ReadLine(); principal = Convert.ToDecimal(sPrincipal); // exit if the value entered is correct if (principal >= 0) { break; } // otherwise generate an error on incorrect input Console.WriteLine("Principal cannot be negative\n"); } // now enter the interest rate decimal interest; while(true) { Console.Write("Enter interest (percent annual): "); string sInterest = Console.ReadLine(); interest = Convert.ToDecimal(sInterest); // don't accept interest that is negative or too large if (interest >= 0 && interest <= maxInterest) { break; } // ...generate an error message as well Console.WriteLine("Interest cannot be negative " + "or greater than " + maxInterest + "\n"); } // now input the years Console.Write("Enter number of years: "); string sDuration = Console.ReadLine(); int duration = Convert.ToInt32(sDuration); // show the input Console.WriteLine(); Console.WriteLine("Principal = " + principal); Console.WriteLine("Interest = " + interest + "%"); Console.WriteLine("Duration = " + duration + " years"); Console.WriteLine(); // output the result Console.WriteLine("Year:" + "\t " + "Value:\n"); // now loop through the specified number of years int year = 1; while(year <= duration) { // calculate the value of the interest decimal interestPaid; interestPaid = principal * (interest / 100); // now calculate the new principal by adding the interest principal += interestPaid; // round off the principal to the nearest cent principal = decimal.Round(principal, 2); // output the result Console.WriteLine(year + "\t $" + principal); // now go to the next year year = year + 1; } Console.WriteLine("\nPress Enter to exit ..."); Console.Read(); } } }