In my previous blog– Making a Change in Greedy, I explained you how we can deal with a Greedy algorithm by making a change example. Today, we will see its program in C#, where I had taken a set of {100, 50, 20, 10, 5 and 1} and our aim is to include a method to input the purchase amount and the amount given by the customer as well as a method to output the amount of change and breakdown by denomination. Apply Greedy algorithm at the cashier side; i.e give fewer numbers of coins to satisfy the Greedy algorithm.
Algorithm
MAKE-CHANGE (n)
C ← {100, 20, 10, 5, 1} // constant.
Sol ← {}; // set that will hold the solution set.
Sum ← 0 sum of item in solution set
WHILE sum not = n
x = largest item in set C such that sum + x ≤ n
IF no such item THEN
RETURN "No Solution"
S ← S {value of x}
sum ← sum + x
RETURN S
Making a Change Problem in C#
Step 1
Open your Visual Studio. By pressing Ctrl +Shift + N, you will get your “New Project” Window.
Step 2
After pressing OK, you will get into your coding part, where you will see three files in Solution Explorer [Properties, References, Program.cs], in which Program.cs file is your main file, where you embed all your making a change program code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace ConsoleApplication1 {
- class Program {
-
- static int note100, return100, note50, return50, note20, return20, note10, return10,
- note5, return5, note1, return1, coin1, returncoins;
- static int billamount = 0, recvamount = 0, differ = 0, change = 0;
-
-
-
- private static void calcNumberNotes() {
- int differ = 0;
-
- differ = recvamount - billamount;
-
-
- return100 = (int)(Math.Floor(differ / 100 m));
- if (return100 > note100) {
- differ = differ - (note100 * 100);
- return100 = note100;
- } else {
- differ = differ - (return100 * 100);
-
- }
-
-
- return50 = (int)(Math.Floor(differ / 50 m));
- if (return50 > note50) {
- differ = differ - (note50 * 50);
- return50 = note50;
- } else {
- differ = differ - (return50 * 50);
-
- }
-
-
- return20 = (int)(Math.Floor(differ / 20 m));
- if (return20 > note20) {
- differ = differ - (note20 * 20);
- return20 = note20;
- } else {
- differ = differ - (return20 * 20);
-
- }
-
-
-
- return10 = (int)(Math.Floor(differ / 10 m));
- if (return10 > note10) {
- differ = differ - (note10 * 10);
- return10 = note10;
- } else {
- differ = differ - (return10 * 10);
-
- }
-
-
- return5 = (int)(Math.Floor(differ / 5 m));
- if (return5 > note5) {
- differ = differ - (note5 * 5);
- return5 = note5;
- } else {
- differ = differ - (return5 * 5);
-
- }
-
-
- return1 = (int)(Math.Floor(differ / 1 m));
- if (return1 > note1) {
- differ = differ - (note1 * 1);
- return1 = note1;
- } else {
- differ = differ - (return1 * 1);
-
- }
-
- if (differ <= coin1) {
-
- returncoins = differ;
-
- } else {
-
- returncoins = -1;
-
- }
-
- }
-
-
-
- static void Main(string[] args) {
-
-
- note100 = note50 = note20 = note10 = note5 = note1 = coin1 = 0;
-
- Console.WriteLine("Enter your 100rs Note available");
- note100 = Convert.ToInt32(Console.ReadLine());
-
- Console.WriteLine("\n");
-
- Console.WriteLine("Enter your 50rs Note available");
- note50 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("\n");
-
- Console.WriteLine("Enter your 20rs Note available");
- note20 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("\n");
-
- Console.WriteLine("Enter your 10rs Note available");
- note10 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("\n");
-
- Console.WriteLine("Enter your 5rs Note available");
- note5 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("\n");
-
- Console.WriteLine("Enter your 1rs Note available");
- note1 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("\n");
-
- Console.WriteLine("Enter your 1rs coint available");
- coin1 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("\n");
-
-
- Console.WriteLine("Enter the Bill Amount :");
- billamount = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("\n");
-
-
-
- Console.WriteLine("Enter the Recieved Amount :");
- recvamount = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("\n");
-
- Console.WriteLine("The Bill Amount is:- \t" + billamount);
- Console.WriteLine("\n");
-
- Console.WriteLine("The Recieved Amount is:- \t" + recvamount);
- Console.WriteLine("\n");
-
-
- calcNumberNotes();
-
-
- if (returncoins < 0) {
- Console.WriteLine("Changes are not Available");
- } else {
- Console.WriteLine("\n");
- change = recvamount - billamount;
- Console.WriteLine("Changes Available Are:- \t" + change);
- }
-
-
-
-
- Console.WriteLine("\n");
-
-
- Console.WriteLine("Notes of 100:\t" + return100);
- Console.WriteLine("Notes of 50:\t" + return50);
- Console.WriteLine("Notes of 20:\t" + return20);
- Console.WriteLine("Notes of 10:\t" + return10);
- Console.WriteLine("Notes of 5:\t" + return5);
- Console.WriteLine("Notes of 1:\t" + return1);
- Console.WriteLine("Coins:\t" + returncoins);
-
- Console.ReadKey();
-
-
- }
- }
- }
Output Window
Hope, you like it. Thank you for reading, Have a good day.