I new to the C# world. I'm currently taking a class and am having trouble with the following program.

"Write a program for an automated teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispensed are 50s, 20s, and 10s. Write a function that determines how many of each kind of bill to dispense."

I think I have the first part figured out where the user enters the amount desired but I don't understand how to write the function that determines how many of each kind of bill to dispense.

Can anyone help me with this?

Thanks!

Dani AI

Generated

A compact, reliable way to solve this is to use integer division and the remainder operator (modulus). That follows ’s suggestion to think in terms of mod/div, keeps everything in integers (avoid doubles), and produces the fewest bills when using 50, 20 and 10 (greedy largest-first is optimal for these denominations). ’s struct idea is a clean return type; if structs aren’t covered yet, returning an int[] or using out parameters works fine. ’s while-loop approach is valid but can be written more simply and efficiently with division and modulo.

// returns [fifties, twenties, tens]
static int[] GetBillCounts(int amount)
{
    if (amount <= 0 || (amount % 10) != 0)
        throw new ArgumentException("Amount must be a positive multiple of 10.");

    int fifties = amount / 50;
    amount %= 50;

    int twenties = amount / 20;
    amount %= 20;

    int tens = amount / 10;

    return new[] { fifties, twenties, tens };
}

A couple of quick notes: validate input with int.TryParse if it comes from Console or UI, and reject non-multiples of 10. Using int avoids rounding pitfalls seen when double is used (as in ’s example). Also, a minor correction to the original example: the breakdown 4×50 + 2×20 + 1×10 equals 250, not 230 — for 230 the function above returns 4 fifties, 1 twenty and 1 ten.

For extra clarity, test edge cases (0, 10, 30, large values) and, once comfortable with the basics, consider returning a small struct or tuple to make the result clearer in calling code (as suggested).

Recommended Answers

All 8 Replies

Hi dennis! Welcome at Daniweb!

You could write a method (C# does not use the word function) with a parameter that inputs the total amount and that returns a struct like this

struct MyStruct
    {
        int AmountOf50s;
        int AmountOf20s;
        int AmountOf10s;
    }

Succes! And show us what you came up with.

Also keep modulus division in mind so you can calculate the quantity of each denomination of money. Danny already gave you the struct data type for storing the denominations calculated

Hi dennis! Welcome at Daniweb!

You could write a method (C# does not use the word function) with a parameter that inputs the total amount and that returns a struct like this

struct MyStruct
    {
        int AmountOf50s;
        int AmountOf20s;
        int AmountOf10s;
    }

Succes! And show us what you came up with.

Thanks for the reply....unfortunately the portion of the class that we are in hasn't covered struct.

I looking more for how to take the input from the user ie $230 and have the program output 4-50s, 2-20s, and 1-10.

This is what I have so far

int
get_amount (void)
{
          int amount, num
          printf ("Enter amount in multiples of 10. \n >");
          if (amt = % 10 = 0)
                    printf ("The amount you wish to withdraw is $%.2f.\n");
         else
                    printf ("The amount entered is not a multiple of 10.\n");
         return (amt)
}

int
get_bills (void)
{
          printf (" Please wait while your transaction is being\n);
          printf (" processed.\n");

This is where I am stuck.

Do you have a C class or a C# class?
If you are studying C you are in the wrong forum. Although C and C# have some similarities, they are quite different languages.
Before some moderator moves this thread(if in fact you are not in C#)
Your get_amount function does input an amount. Use the scanf, I think it is called.

That looks very much like C rather than C#.

I think if (amt = % 10 = 0) should read if( (amt % 10) == 0) it looks to me like you are assigning the modulo value to amt though I am unsure if this is your intention.

The maths you need are quite simple, first calculate the number of 50s you need and take away the value of those and do the same for 20s and then 10s.

Example for 50s:

if(amt >= 50)
{
    fiftys = (amt - (amt % 50))/50;
    amt -= 50 * fiftys;
}


In C# dividing two integers is an integer divide, so 230/50 would give 4.
The calculation could thus be simplified. I think in C it is the same, but I'm not sure anymore. In the case it is not, then your approach is the way to go.

<Dusts off a dog eared copy of Kerninghan & Ritchie>
In C '%' is illeagal with floats and '/' with integers truncates any fractional part.

So you are absolutely correct! Maybe I should have applied a little more thought to it.

Here is a simple method that I knocked up that might help you

private void Dispense(string inputAmount)
        {
            int fifty_ammount = 0;
            int twenty_ammount = 0;
            int ten_ammount = 0;
            int five_ammount = 0;

            double total = Convert.ToDouble(inputAmount);

            while ((total - 50) >= 0)
            {
                fifty_ammount += 1;
                total -= 50;
            }
            while ((total - 20) >= 0)
            {
                twenty_ammount += 1;
                total -= 20;
            }
            while ((total - 10) >= 0)
            {
                ten_ammount += 1;
                total -= 10;
            }
            while ((total - 5) >= 0)
            {
                five_ammount += 1;
                total -= 5;
            }

            //print out the amounts

        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.