Create a new project, and name it P1T7 Write a program that requests from a user the number of pies (at R18.50 per pie) and the number of hamburgers (at R35.00 per hamburger), and then calculates the total for the order. Now extend this program to request the amount paid by the user, before calculating the change that needs to be paid out. Challenge: Can you calculate and display the exact coins and notes that are needed to make up the change (least amount of notes and coins)? For example, R13.50 change will need the following: 1xR10.00, 1xR2.00, 1xR1.00 and 1x50c
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_SHARP
{
class Program
{
static void Main(string[] args)
{
const decimal PIE_PRICE = 18.50M;
const decimal HAMBURGER_PRICE=35.00M;
//requests from a user the number of pies
Console.Write("Enter the number of pies (at R18.50 per pie): ");
int numberPies = int.Parse(Console.ReadLine());
Console.Write("Enter the number of hamburgers (at R35.00 per hamburger): ");
int numberHamburgers = int.Parse(Console.ReadLine());
// calculates the total for the order.
decimal totalOrder = numberPies * PIE_PRICE + numberHamburgers * HAMBURGER_PRICE;
Console.WriteLine("The total for the order: R{0}", totalOrder.ToString("N"));
//request the amount paid by the user, before calculating the change that needs to be paid out
decimal amountPaid = -1;
while (amountPaid < totalOrder) {
Console.Write("Enter the amount paid: ");
amountPaid = decimal.Parse(Console.ReadLine());
}
//For example, R13.50 change will need the following: 1xR10.00, 1xR2.00, 1xR1.00 and 1x50c
decimal change = amountPaid - totalOrder;
Console.WriteLine("The change: R{0}", change.ToString("N"));
int intChange = (int)(change * 100);
// coins and notes
int notes10 = intChange / 1000;
intChange %= 1000;
int notes5 = intChange / 500;
intChange %= 500;
int notes2 = intChange / 200;
intChange %= 200;
int notes1 = intChange / 100;
intChange %= 100;
int half = intChange / 50;
intChange %= 50;
int quarterCoins = intChange / 25;
intChange %= 25;
int tenCoins = intChange / 10;
intChange %= 10;
int fiveCoins = intChange / 5;
int oneCoin = intChange % 5;
if (notes10 > 0)
{
Console.WriteLine("{0}xR10.00", notes10);
}
if (notes5 > 0)
{
Console.WriteLine("{0}xR5.00", notes5);
}
if (notes2 > 0)
{
Console.WriteLine("{0}xR2.00", notes2);
}
if (notes1 > 0)
{
Console.WriteLine("{0}xR1.00", notes1);
}
if (half > 0)
{
Console.WriteLine("{0}x50 coins", half);
}
if (quarterCoins > 0)
{
Console.WriteLine("{0}x25 coins", quarterCoins);
}
if (tenCoins > 0)
{
Console.WriteLine("{0}x10 coins", tenCoins);
}
if (fiveCoins > 0)
{
Console.WriteLine("{0}x5 coins", fiveCoins);
}
if (oneCoin > 0)
{
Console.WriteLine("{0}x1 coin", oneCoin);
}
Console.ReadLine();
}
}
}
Example:
Comments
Leave a comment