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:
Enter the price
44
Enter the money given
100
Your change is 56
Number of 50s returned is 1
Number of 5s returned is 1
Number of 1s returned is 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Giving_Change
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the price");
int price = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the money given");
int moneyGiven = int.Parse(Console.ReadLine());
int change = moneyGiven - price;
string message = "Your change is " + change + "\n";
int fifty = 0;
int twenty = 0;
int ten = 0;
int five = 0;
int two = 0;
int one = 0;
string msg50 = "";
string msg20 = "";
string msg10 = "";
string msg5 = "";
string msg2 = "";
string msg1 = "";
while (change > 0)
{
if (change >= 50)
{ change -= 50;
fifty += 1;
msg50 = "Number of 50s returned is " + fifty + "\n";
}
if (change >= 20)
{
change -= 20;
twenty += 1;
msg20 = "Number of 20s returned is " + twenty + "\n";
}
if (change >= 10)
{
change -= 10;
ten += 1;
msg10 = "Number of 10s returned is " + ten + "\n";
}
if (change >= 5)
{
change -= 5;
five += 1;
msg5 = "Number of 5s returned is " + five + "\n";
}
if (change >= 2)
{
change -= 2;
two += 1;
msg2 = "Number of 2s returned is " + two + "\n";
}
if (change == 1)
{
change -= 1;
one += 1;
msg1 = "Number of 1s returned is " + one;
}
}
string answer = message + msg50 + msg20 + msg10 + msg5 + msg2 + msg1;
Console.WriteLine(answer);
Console.Read();
}
}
}
No comments:
Post a Comment