•
•
•
•
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
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(); } } }
Comments (Newest First)
Lardmeister | Nearly a Posting Virtuoso | Apr 1st, 2007
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.
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
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
SnippetCompiler.exe
from:
http://www.sliver.com/dotnet/SnippetCompiler/