Calculating the Note Change in C#


using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Text.RegularExpressions;

namespace Currency
{
class Program
{
static void Main(string[] args)             //Main Method
{
List<int> notes = new List<int>();
List<int> amounts = new List<int>() { 10, 20, 50, 100, 500 ,1000, 5000 };

BACK:
Console.Write(“Enter input : “); // Prompt
int line = Convert.ToInt32(Console.ReadLine());
Change(notes, amounts, 0, 0, line);
Console.ReadKey();
goto BACK;

}
static void Change(List<int> notes, List<int> amounts, int highest, int sum, int goal)
{
//
// See if we are done.
//
if (sum == goal)
{
Display(notes, amounts);
return;
}
//
// See if we have too much.
//
if (sum > goal)
{
return;
}
//
// Loop through amounts.
//
foreach (int value in amounts)
{
//
// Only add higher or equal amounts.
//
if (value >= highest)
{
List<int> copy = new List<int>(notes);
copy.Add(value);
Change(copy, amounts, value, sum + value, goal);
}
}
}

static void Display(List<int> notes, List<int> amounts)
{
foreach (int amount in amounts)
{
int count = notes.Count(value => value == amount);
Console.WriteLine(“{0} x {1}”,amount,count);
}
Console.WriteLine();
}
}
}

 

Output:

money

Advertisements

It's Great To Hear From You

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s