Interest Paid on Savings

Lardmeister 0 Tallied Votes 157 Views Share

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".

// 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();
    }
  }
}
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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.

Lardmeister 461 Posting Virtuoso

You can run these short C# snippets with the free GUI based
SnippetCompiler.exe
from:
http://www.sliver.com/dotnet/SnippetCompiler/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.