944,126 Members | Top Members by Rank

Ad:
  • C# Code Snippet
  • Views: 4277
  • C# RSS
0

Interest Paid on Savings

by on Mar 30th, 2007
A very simple C# console program to give you a table of investment (savings) growth over the years. To compile the program, copy and paste the code into an editor, and save it for instance as "savings.cs". Then find the C# compiler "csc.exe" in the "C:\Windows\Microsoft.NET\Framework\ ..." folder and run the command line (you might have to specify the folders too):
csc.exe savings.cs
That should produce a file "savings.exe".
C# Code Snippet (Toggle Plain Text)
  1. // calculate interest paid annually on a given principle over
  2. // a period of specified years and show a table of the results
  3. // this is a console program
  4. // compiled with C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
  5.  
  6. using System;
  7.  
  8. namespace CalcInterestTable
  9. {
  10. using System;
  11.  
  12. public class InterestTable
  13. {
  14. public static void Main()
  15. {
  16. // define a maximum interest rate
  17. int maxInterest = 100;
  18.  
  19. Console.Write("Program will ask you for the starting amount\n");
  20. Console.Write("you deposit, the interest paid and the number\n");
  21. Console.Write("of years you want the investment to last ...\n\n");
  22.  
  23. // keep prompting until you get the proper value
  24. decimal principal;
  25. while(true)
  26. {
  27. Console.Write("Enter starting principal amount: ");
  28. string sPrincipal = Console.ReadLine();
  29. principal = Convert.ToDecimal(sPrincipal);
  30.  
  31. // exit if the value entered is correct
  32. if (principal >= 0)
  33. {
  34. break;
  35. }
  36.  
  37. // otherwise generate an error on incorrect input
  38. Console.WriteLine("Principal cannot be negative\n");
  39. }
  40.  
  41. // now enter the interest rate
  42. decimal interest;
  43. while(true)
  44. {
  45. Console.Write("Enter interest (percent annual): ");
  46. string sInterest = Console.ReadLine();
  47. interest = Convert.ToDecimal(sInterest);
  48.  
  49. // don't accept interest that is negative or too large
  50. if (interest >= 0 && interest <= maxInterest)
  51. {
  52. break;
  53. }
  54.  
  55. // ...generate an error message as well
  56. Console.WriteLine("Interest cannot be negative " +
  57. "or greater than " + maxInterest + "\n");
  58. }
  59.  
  60. // now input the years
  61. Console.Write("Enter number of years: ");
  62. string sDuration = Console.ReadLine();
  63. int duration = Convert.ToInt32(sDuration);
  64.  
  65. // show the input
  66. Console.WriteLine();
  67. Console.WriteLine("Principal = " + principal);
  68. Console.WriteLine("Interest = " + interest + "%");
  69. Console.WriteLine("Duration = " + duration + " years");
  70. Console.WriteLine();
  71.  
  72. // output the result
  73. Console.WriteLine("Year:" + "\t " + "Value:\n");
  74. // now loop through the specified number of years
  75. int year = 1;
  76. while(year <= duration)
  77. {
  78. // calculate the value of the interest
  79. decimal interestPaid;
  80. interestPaid = principal * (interest / 100);
  81.  
  82. // now calculate the new principal by adding the interest
  83. principal += interestPaid;
  84.  
  85. // round off the principal to the nearest cent
  86. principal = decimal.Round(principal, 2);
  87.  
  88. // output the result
  89. Console.WriteLine(year + "\t $" + principal);
  90.  
  91. // now go to the next year
  92. year = year + 1;
  93. }
  94.  
  95. Console.WriteLine("\nPress Enter to exit ...");
  96. Console.Read();
  97. }
  98. }
  99. }
Comments on this Code Snippet
Mar 31st, 2007
0

Re: Interest Paid on Savings

Lardmeister, nice of you to point out the C# programs don't necessarily need Visual C# to run. I can run your code from my ConText general programming IDE, just have to tell it where csc.exe is located. Good info to have!

I can also run it from the SharpDevelop IDE, but then I have to start a new solution, and get all those extra files.
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Apr 1st, 2007
0

Re: Interest Paid on Savings

You can run these short C# snippets with the free GUI based
SnippetCompiler.exe
from:
http://www.sliver.com/dotnet/SnippetCompiler/
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Message:
Previous Thread in C# Forum Timeline: Factorial Numbers
Next Thread in C# Forum Timeline: Help, I just have a C# book.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC