User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 402,503 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,743 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Mar 30th, 2007
Views: 2,279
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".
csharp Syntax | 5 stars
  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 (Newest First)
Lardmeister | Nearly a Posting Virtuoso | Apr 1st, 2007
You can run these short C# snippets with the free GUI based
SnippetCompiler.exe
from:
http://www.sliver.com/dotnet/SnippetCompiler/
vegaseat | Kickbutt Moderator | Mar 31st, 2007
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.
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC