Jeremy Shanks
Spread the Knowledge!

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.


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.

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.

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.

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.

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.

About the Author Jeremy Shanks

Hi, my name is Jeremy Shanks. I am a Software Engineer in Raleigh, NC. Recently I embarked on the journey of teaching others how to develop projects in C#. Be sure to check them out if you are new to C#.

follow me on: