| | |
Interest Paid on Savings
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".
csc.exe savings.cs
That should produce a file "savings.exe".
// 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(); } } }
0
•
•
•
•
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.
I can also run it from the SharpDevelop IDE, but then I have to start a new solution, and get all those extra files.
0
•
•
•
•
You can run these short C# snippets with the free GUI based
SnippetCompiler.exe
from:
http://www.sliver.com/dotnet/SnippetCompiler/
SnippetCompiler.exe
from:
http://www.sliver.com/dotnet/SnippetCompiler/
Similar Threads
Other Threads in the C# Forum
- Huge Savings (PHP)
- Savings Calculator (JavaScript / DHTML / AJAX)
Other Threads in the C# Forum
- Previous Thread: Factorial Numbers
- Next Thread: Help, I just have a C# book.
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access algorithm application array asp.net bitmap box button c# check checkbox class code color combo combobox connectionproblem control conversion csharp custom data database datagrid datagridview dataset degrees directshow display dll drawing event excel expression file form format forms ftp function game gcd gdi+ graphics image index input install internet label list listbox listener login math mysql object operator photoshop picturebox print programming recursion remote remoting resource resourcefile round saving search server socket sql sql-server start statistics stream string text textbox thread threading time timer treeview tutorial update validation vc++ video view visual webbrowser webcam windows winforms wpf xml



