Currency destruction in C #

I am trying to get this program to break a user-defined amount of dollars into the smallest possible accounts. I don’t think my for loop works, because if I put a line of string in them, it does not appear 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]);
        }
    }
}
+3
source share
5 answers

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;
    }
+2
source

This is a homework question, right?

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

do it

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

and try again :) The central part is the condition in which the loop should work.

+2
source

, " ", , , .

"" ", " , . , , - , for-loop, , , .

, modulo).

+1
source

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

Change it to

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

Try this, a little 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;
        }
    }
0
source

Source: https://habr.com/ru/post/1740740/


All Articles