Performing Calculations in C#

Our “Hello, World” program illustrated the basic structure of a C# program and introduced classes and components, but we need a slightly more elaborate example to show the use of other basic programming constructs, such as variables, expressions, and control structures. Our next example is a simple calculator for a savings account. The user is prompted for annual contribution, interest rate, and number of years. We calculate the accumulation of deposits two ways:

  • In a loop, year by year, accumulating a total as we go

  • Using a formula

The example program is in the folder Savings.

 // Savings.cs using System; class Savings { public static void Main(string[] args) { InputWrapper iw = new InputWrapper(); decimal amount; ...

Get Programming PERL in the .NET Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.