-6

I'm doing a school project and i need to know how to Convert user-entered dollars into $100 bill and $1000 bill and dollars remaining. let's say your in the store and you want to pay your items with as high bill as possible so let's say you have to pay $9853. How do i print how many $1bills, $5 bills, $10 bills, $50 bills, $100 bills and 1000$ bills that is?

PAY WITH AS HIGH BILL AS YOU CAN. NO CHANGE.

$9853 would be: 9x $1000 bills, 8x $100 bills, 1x $50 bill and 3x $1 bills.

if anyone has an answer to this please answer, any answer is very helpful. Thanks.

Realitiez
  • 489
  • 2
  • 6
  • 12

3 Answers3

4

$9853 would be: 9x $1000 bills, 8x $100 bills, 1x $50 bill and 3x $1 bills.

How did you do this in your head? Let's think about it:

You probably started with the thousand-dollar bills. You want to use as many as possible. How many is that? Divide $9853 by 1000, and throw away the remainder.

int thousands = amount / 1000;

How much money do we still need to pay? The remainder of the division we just did, which is $853.

amount %= 1000;

Now, we want to use as many hundreds as possible. How many? 8, because $853 / 100 is 8 (throwing away the remainder).

int hundreds = amount / 100;

And now how much is left over? The remainder:

amount %= 100;

Does this suggest an algorithm to you? Do you think you could fill in the rest of the code from here?

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • 1
    Thanks for the answer, just what i needed, i just needed to know the basic usage of it within the program, thanks a lot. – Realitiez Feb 13 '14 at 17:59
0

You can use the remainder(%-modulus)

Let us consider the amount to be $9853

`First:
9853 / 1000 gives 9(this means 9 - $1000 bills)

9853 % 1000 gives 853

853 / 100 gives 8(this means 8 - $100 bills)

853 % 100 gives 53

53 / 10 gives 5 (here check the answer if the answer is 5 or greater than 5 then use $50 bill and the remaining $10 bill. Like if the answer is 30 then you get 3 - $10 bill and if 6 then one $50 bill and 1 - $10 bill)

53 % 10 gives 3(here again check your answer if greater than 5 use $5 bill and then the left $1 bill)

Hope this might help

Bhavik
  • 4,836
  • 3
  • 30
  • 45
0

Try this.It's very simple.....

 public void CountDollers(int number) {
            int[] values = new int[] { 1000, 100, 50,1 }; // declare here your counter serise            
            int i = 0 ,rem = 0;
            do
            {
                rem = number / values[i];
                Console.WriteLine("Number of "+values[i]+"$ "+ rem);
                number = number % values[i];                             
                i++;
            } while (number > values[values.Length-1]-1);            
        }

Usage :

CountDollers(9853);
Elshan
  • 7,339
  • 4
  • 71
  • 106