Programming Course in C# ¡Free!

Give change

 Friday, April 05, 2013 published by Exercises C#
Proposed exercise

Create a C# program to return the change of a purchase, using coins (or bills) as large as possible. Assume we have an unlimited amount of coins (or bills) of 100, 50, 20, 10, 5, 2 and 1, and there are no decimal places. Thus, 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


Output



Solution


using System;
public class GiveChange
{
public static void Main()
{
Console.Write("Price? ");
int price = Convert.ToInt32(Console.ReadLine());

Console.Write("Paid? ");
int paid = Convert.ToInt32(Console.ReadLine());

int change = paid - price;
Console.Write("Your change is {0}: ", change);

while ( change >= 50 )
{
Console.Write("50 ");
change -= 50;
}

while ( change >= 20 )
{
Console.Write("20 ");
change -= 20;
}

while ( change >= 10 )
{
Console.Write("10 ");
change -= 10;
}

while ( change >= 5 )
{
Console.Write("5 ");
change -= 5;
}

while ( change >= 2 )
{
Console.Write("2 ");
change -= 2;
}

while ( change >= 1 )
{
Console.Write("1 ");
change -= 1;
}

Console.WriteLine();
}
}