public
Created

[01/28/13] Challenge #119 [Easy] Change Calculator

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ChangeCalculator
{
class Program
{
static int quarter = 0;
static int dime = 0;
static int nickel = 0;
static int penny = 0;
static void Main(string[] args)
{
Console.Write("Enter Amount ($): ");
string input = Console.ReadLine();
decimal input_amount;
bool result = Decimal.TryParse(input, out input_amount);
Change(input_amount * 100);
Console.WriteLine("Quarterers: " + quarter + "\nDimes: " + dime + "\nNickels: " + nickel + "\nPennies: " + penny);
Console.ReadLine();
}
//Recursive (i think) Change() method, accepting 1 argument(decimal amount)
static void Change(decimal amount)
{
if (amount >= 25)
{
amount = amount - 25;
quarter++;
Change(amount);
}
else if (amount >= 10)
{
amount = amount - 10;
dime++;
Change(amount);
}
else if (amount >= 5)
{
amount = amount - 5;
nickel++;
Change(amount);
}
else if (amount >= 1)
{
amount = amount - 1;
penny++;
Change(amount);
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.