Covertpyro
Covertpyro

Reputation: 199

Breaking down currency in c#

i'm trying to get this program to break down a user defined amount of dollars into the fewest possible bills. i don't think my for loops are running because if i put a writeline line in them it doesn't show up when i run it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication13
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.Write("Enter the amount of money: $");
            int totalAmount = Convert.ToInt32(Console.ReadLine());
            calculateNumberOfBills(totalAmount);
        }

        static void calculateNumberOfBills(int totalAmount)
        {

            int[] denominations = { 20, 10, 5, 1 };
            int[] numberOfBills = new int[4];
            for (numberOfBills[0] = 0; totalAmount < 20; numberOfBills[0]++)
            {
                totalAmount = totalAmount - 20;
            }
            for (numberOfBills[1] = 0; totalAmount < 10; numberOfBills[1]++)
            {
                totalAmount = totalAmount - 10;
            }
            for (numberOfBills[2] = 0; totalAmount < 5; numberOfBills[2]++)
            {
                totalAmount = totalAmount - 5;
            }
            for (numberOfBills[3] = 0; totalAmount <= 0; numberOfBills[3]++)
            {
                totalAmount = totalAmount - 1;
            }
            Console.WriteLine("Number of twenties" + numberOfBills[0]);
            Console.WriteLine("Number of tens" + numberOfBills[1]);
            Console.WriteLine("Number of fives" + numberOfBills[2]);
            Console.WriteLine("Number of ones" + numberOfBills[3]);
        }
    }
}

Upvotes: 3

Views: 3956

Answers (5)

Rasshme
Rasshme

Reputation: 1641

Try this, a bit of less code

    int amt = 73;

    Dictionary<int, int> dic = new Dictionary<int, int>() {{20,0},{10,0},{5,0},{1,0}};
    int[] keys =new  int[dic.Count];
    dic.Keys.CopyTo(keys, 0);

    foreach (int i in  keys)
    {            
        if (amt >= i)
        {
            dic[i] = amt / i;
            amt = amt % i;
        }
    }

Upvotes: 0

zs2020
zs2020

Reputation: 54514

Take a look at this:

for (numberOfBills[0] = 0; totalAmount >= 20; numberOfBills[0]++)
    {
        totalAmount = totalAmount - 20;
    }
    for (numberOfBills[1] = 0; totalAmount >= 10; numberOfBills[1]++)
    {
        totalAmount = totalAmount - 10;
    }
    for (numberOfBills[2] = 0; totalAmount >= 5; numberOfBills[2]++)
    {
        totalAmount = totalAmount - 5;
    }
    for (numberOfBills[3] = 0; totalAmount > 0; numberOfBills[3]++)
    {
        totalAmount = totalAmount - 1;
    }

Upvotes: 2

Johannes Rudolph
Johannes Rudolph

Reputation: 35721

Sorry, at this point this is "not a real question" and should most probably be tagged as homework.

Your "smaller than" comparisons should be changed to "greater than" comparisons and you're good to go with your solution. What's happening now is an integer overflow, eventually causing your first for-loop , which was infinitely looping until then to break.

There are easier ways to solve your problem, try it with a single loop and the modulo operator).

Upvotes: 1

J-16 SDiZ
J-16 SDiZ

Reputation: 26910

This is a homework question, right?

        for (numberOfBills[0] = 0; totalAmount < 20; numberOfBills[0]++)

make it

        for (numberOfBills[0] = 0; totalAmount >= 20; numberOfBills[0]++)

and try again :) The center piece is the condition where the loop should run.

Upvotes: 2

Axarydax
Axarydax

Reputation: 16603

in your "for" loops you have inverse condition => totalAmount < 20 means that it executes loop while totalAmount is less than 20 - that is the opposite of what you want.

Change it to

for(...;totalAmount > 20; ...)

Upvotes: 0

Related Questions