How to Create a Change Calculator in C#

In this tutorial, I will show you how to create a simple change calculator in C# using Visual Studio. I will show you how to take any dollar amount and calculate exactly how many bills and coins to give back. By the end of this tutorial, you will be able to run your change calculator and calculate the bills and coins (in US dollars) that amount equals.

Requirements

Difficulty: Easy

Concepts Used:

We want to create a change calculator that can tell the user how many bills and coins of each denomination to count out for any given total dollar amount. In this tutorial, we will be randomly generating a dollar amount up to $1000 and sending it to the change calculator. We will then print out the quantity of each denomination adding up to that total.

Let’s Get Started

To get started, we need to create an empty C# console application in Visual Studio. For more information on how to do this, check out my walkthrough for creating a new console application in Visual Studio.

Now that we have our new blank console application we are ready to get started with our project. The first thing we need to do is create a “Change” class that will do the calculations and store the values. We will create this class outside the “Program” class. Inside this class, we will create a public integer variable with only a getter for each denomination of the dollar system. For example, five dollar bills, dollar bills, quarters, etc. Take a look at the following code.

public class Change
    {
        public int HundredDollarBills { get; }
        public int FiftyDollarBills { get; }
        public int TwentyDollarBills { get; }
        public int TenDollarBills { get; }
        public int FiveDollarBills { get; }
        public int DollarBills { get; }
        public int Quarters { get; }
        public int Dimes { get; }
        public int Nickels { get; }
        public int Pennies { get; }
    }

In the above code, you will see we have a property with a getter associated with it for each denomination of the US dollar system. Now it’s time to add a constructor to the Change class. In this constructor, we want to calculate the number of each denomination the amount equals. Take a look at the following code to see how we do this.

public Change(decimal price)
{
    HundredDollarBills = (int)(price / 100);
    price %= 100;

    FiftyDollarBills = (int)(price / 50);
    price %= 50;

    TwentyDollarBills = (int)(price / 20);
    price %= 20;

    TenDollarBills = (int)(price / 10);
    price %= 10;

    FiveDollarBills = (int)(price / 5);
    price %= 5;

    DollarBills = (int)(price / 1);
    price %= 1;

    Quarters = (int)(price / .25m);
    price %= .25m;

    Dimes = (int)(price / .10m);
    price %= .10m;

    Nickels = (int)(price / .05m);
    price %= .05m;

    Pennies = (int)(price / .01m);
}

In the above code, we have the constructor taking the price in as a parameter. Based on that price, we are assigning the number of each denomination to the property. We are subtracting that amount from the total price by using the modulus operator and then doing the same thing for the next lower denomination until we reach the lowest denomination.

That is it for creating the change calculator class so now we will test it out. I chose to generate a random price up to $1,000.00 and then create a change object with that price. Then I am writing to the console the amount of each denomination. Check out the code below to see how I did this.

decimal price = new Random().Next(0, 100000)/100m;
Console.WriteLine($"The Calculated Price is: {price:C}");
Change change = new Change(price);

Console.WriteLine($"Hundred Dollar Bills: {change.HundredDollarBills}");
Console.WriteLine($"Fifty Dollar Bills: {change.FiveDollarBills}");
Console.WriteLine($"Twenty Dollar Bills: {change.TwentyDollarBills}");
Console.WriteLine($"Ten Dollar Bills: {change.TenDollarBills}");
Console.WriteLine($"Five Dollar Bills: {change.FiveDollarBills}");
Console.WriteLine($"Dollar Bills: {change.DollarBills}");
Console.WriteLine($"Quarters: {change.Quarters}");
Console.WriteLine($"Dimes: {change.Dimes}");
Console.WriteLine($"Nickels: {change.Nickels}");
Console.WriteLine($"Pennies: {change.Pennies}");

Now we can run this application and see that we are generating a random price amount and then calculating the amount of each denomination to get that amount in money. See below for what this looks like when we run this application.

The Calculated Price is: $286.30
Hundred Dollar Bills: 2
Fifty Dollar Bills: 1
Twenty Dollar Bills: 1
Ten Dollar Bills: 1
Five Dollar Bills: 1
Dollar Bills: 1
Quarters: 1
Dimes: 0
Nickels: 1
Pennies: 0

Congratulations! You have created a change calculator that tells the user exactly how many bills and coins that adds up to the price amount. You have also gotten a chance to work with variables, operators, and expressions in C#. As always, if you have any questions or comments let me know below. If you would like to see more tutorials like this check out my C# Roadmap.

[thrive_leads id=’1299′]