Give Change - C# Programming Exercise

This exercise in C# aims to develop a program that calculates the change for a purchase, using the largest possible coins or bills. The program should ask the user for the purchase price and the amount paid, then calculate the change, broken down into bills or coins of 100, 50, 20, 10, 5, 2, and 1. The change should be given using the maximum possible amount of each denomination, starting with the highest value and working down to the smallest.

This type of exercise is useful for practicing conditionals and loops, as well as working with mathematical calculations and implementing algorithms in programming. The challenge is to find an efficient way to divide the change into the available coins and bills without performing unnecessary calculations. This type of problem is common in payment and transaction management applications, and learning how to handle change is crucial for solving division and modulus related programming issues.

 Category

Flow Control

 Exercise

Give Change

 Objective

Write a C# program to give change for a purchase, using the largest possible coins (or bills). Suppose we have an unlimited amount of coins (or bills) of 100, 50, 20, 10, 5, 2, and 1, and there are no decimals. Therefore, the execution could be something like this:

Price? 44
Paid? 100
Your change is 56: 50 5 1
Price? 1
Paid? 100
Your change is 99: 50 20 20 5 2 2

 Write Your C# Exercise

using System;  // Import the System namespace, which contains fundamental classes like Console

class Program  // Define the Program class
{
    static void Main()  // The entry point of the program
    {
        // Ask the user for the price of the item
        Console.Write("Price? ");
        int price = int.Parse(Console.ReadLine());  // Read and convert the price to an integer

        // Ask the user for the amount paid
        Console.Write("Paid? ");
        int paid = int.Parse(Console.ReadLine());  // Read and convert the paid amount to an integer

        // Calculate the change to be returned
        int change = paid - price;  // Subtract the price from the paid amount to calculate the change

        // Display the total change to be returned
        Console.WriteLine($"Your change is {change}:");

        // Define an array of available coin and bill denominations
        int[] denominations = { 100, 50, 20, 10, 5, 2, 1 };  // Coins and bills in descending order

        // Iterate through the denominations array
        foreach (int denomination in denominations)  
        {
            // Calculate how many coins/bills of this denomination can be given as change
            while (change >= denomination)  
            {
                Console.Write(denomination + " ");  // Display the current denomination
                change -= denomination;  // Subtract the denomination from the remaining change
            }
        }

        // Print a newline after displaying the change
        Console.WriteLine();
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Exceptions

    This exercise in C# aims to develop a program that asks the user for two numbers and displays their division. The program should handle potential errors...

  •  Positive and negative

    In this C# exercise, you will learn how to create a program that determines whether a number entered by the user is positive or negative. The program will prom...

  •  Multiply if not zero

    In this C# exercise, you will learn how to create a program that asks the user for a number. If the entered number is not zero, the program will ask for a second number and ...

  •  Divide if not zero

    In this C# exercise, you will learn how to create a program that asks the user for two numbers. If the second number is not zero, the program will perform the division...

  •  Divide if not zero (Using else)

    In this C# exercise, you will learn how to modify the previous program using the else control structure. The program will ask the user for two numbers, and if the sec...

  •  Greatest of three numbers

    In this C# exercise, you will learn how to write a program that prompts the user to enter three numbers and displays the greatest one. The program will use conditional struc...