Splitting money.

Hello,
i want to make this program but i dont knwo where to begin ...

the program should ask for an amount (money) en then it should give me the given amount  in the shortest possible way

for example : 105,20 euro

the program should say
1X 100
1x 5
1 x 20 cent

here is what i have already


using System;

namespace ConsAppl1TI33 {
  class ConsCode {
    public void ExecuteProgram() {
      decimal Bedrag;
      uint Nr500, Nr200, Nr100, Nr50, Nr20, Nr10, Nr5, Nr2, Nr1, Nr50c, Nr20c, Nr10c, Nr5c, Nr2c, Nr1c;
      ClsMoney Cm = new ClsMoney();
      Console.Title = "Opgave 10: Splitsing geldbedrag (zonder tabel)";
      Bedrag = GiveAmount();
      Cm.SplitAmount(Bedrag, out Nr500, out Nr200, out Nr100, out Nr50, out Nr20, out Nr10, out Nr5,
      out Nr2, out Nr1, out Nr50c, out Nr20c, out Nr10c, out Nr5c, out Nr2c, out Nr1c);
      DisplayResult(Bedrag, Nr500, Nr200, Nr100, Nr50, Nr20, Nr10, Nr5,
      Nr2, Nr1, Nr50c, Nr20c, Nr10c, Nr5c, Nr2c, Nr1c);
    }/*ExecuteProgram*/
    private decimal GiveAmount() {

      Console.WriteLine("Geef een bedrag in Euro in a.u.b. ... ");
      decimal bedrag = decimal.Parse(Console.ReadLine());
      return bedrag;
    }/*GiveAmount*/
    private void DisplayResult(decimal Bedrag, uint Nr500, uint Nr200, uint Nr100, uint Nr50, uint Nr20, uint Nr10, uint Nr5, uint Nr2, uint Nr1, uint Nr50c, uint Nr20c, uint Nr10c, uint Nr5c, uint Nr2c, uint Nr1c) {

      Console.WriteLine("Een bedrag van {} Euro wordt het kortst verdeeld als:\n ", Bedrag);
      Console.WriteLine("{0} X 500",Nr500);
      Console.WriteLine("{0} X 200", Nr200);
      Console.WriteLine("{0} X 100", Nr100);
      Console.WriteLine("{0} X 50", Nr50);
      Console.WriteLine("{0} X 20", Nr20);
      Console.WriteLine("{0} X 10", Nr10);
      Console.WriteLine("{0} X 5", Nr5);
      Console.WriteLine("{0} X 2", Nr2);
      Console.WriteLine("{0} X 1", Nr1);
      Console.WriteLine("{0} X 0,50", Nr50c);
      Console.WriteLine("{0} X 0,20", Nr20c);
      Console.WriteLine("{0} X 0,10", Nr10c);
      Console.WriteLine("{0} X 0,5", Nr5c);
      Console.WriteLine("{0} X 0,2", Nr2c);
      Console.WriteLine("{0} X 0,1", Nr1c);
    }/*DisplayResult*/
  }/*ConsCode*/
}/*ConsAppl1TI33*/

Open in new window


using System;

namespace ConsAppl1TI33 {
  class ClsMoney {
    public void SplitAmount(decimal Bedrag, out uint Nr500, out uint Nr200, out uint Nr100, out uint Nr50, out uint Nr20, out uint Nr10, out uint Nr5,
      out  uint Nr2, out uint Nr1, out uint Nr50c, out uint Nr20c, out uint Nr10c, out uint Nr5c, out uint Nr2c, out uint Nr1c) {
      decimal kopie = Bedrag;
      while (Bedrag >= 500) {

        Nr500++;
        Bedrag = Bedrag / 500;


      } while (Bedrag >= 200) {

        Nr200++;
        Bedrag = Bedrag / 200;


      } while (Bedrag >= 200) {

        Nr200++;
        Bedrag = Bedrag / 200;


      } while (Bedrag >= 100) {

        Nr100++;
        Bedrag = Bedrag / 100;


      } while (Bedrag >= 50) {

        Nr50++;
        Bedrag = Bedrag / 50;


      } while (Bedrag >= 20) {

        Nr20++;
        Bedrag = Bedrag / 20;


      } while (Bedrag >= 10) {

        Nr10++;
        Bedrag = Bedrag / 10;


      } while (Bedrag >= 5) {

        Nr5++;
        Bedrag = Bedrag / 5;

      } while (Bedrag >= 2) {

        Nr2++;
        Bedrag = Bedrag / 2;

      } while (Bedrag >= 2) {

        Nr2++;
        Bedrag = Bedrag / 2;

      } while (Bedrag >= 1) {

        Nr1++;
        Bedrag = Bedrag / 1;

      } while (Bedrag >= 1) {

        Nr1++;
        Bedrag = Bedrag / 1;

      } while (Bedrag >= 1) {

        Nr1++;
        Bedrag = Bedrag / 1;

      } while (Bedrag >= 0.50) {

        Nr50c++;
        Bedrag = Bedrag / 0.50;

      } while (Bedrag >= 0.5) {

        Nr5c++;
        Bedrag = Bedrag / 0.5;

      } while (Bedrag >= 0.1) {

        Nr1c++;
        Bedrag = Bedrag / 0.1;

      }


    }/*ClsMoney*/
  }/*ClsMoney*/
}/*ConsAppl1TI33*/

Open in new window

thanks
kensy11Asked:
Who is Participating?
 
Gustav BrockConnect With a Mentor CIOCommented:
Modulus doesn't work with integers.
I think you need something like this (for your homework!?):

using System;

namespace ConsAppl1TI33 {
  class ClsMoney {
    public void SplitAmount(decimal Bedrag, out uint Nr500, out uint Nr200, out uint Nr100, out uint Nr50, out uint Nr20, out uint Nr10, out uint Nr5,
      out  uint Nr2, out uint Nr1, out uint Nr50c, out uint Nr20c, out uint Nr10c, out uint Nr5c, out uint Nr2c, out uint Nr1c) {
      decimal kopie = Bedrag;
      decimal geld;
      geld = 500;
      while (Bedrag >= geld) {
        Nr500++;
        Bedrag = Bedrag - geld;
      } 
      geld = 200;
      while (Bedrag >= geld) {
        Nr200++;
        Bedrag = Bedrag - geld;
      } 
      geld = 100;
      while (Bedrag >= geld) {
        Nr100++;
        Bedrag = Bedrag - geld;
      } 

// snip

      geld = 0.5;
      while (Bedrag >= geld) {
        Nr50c++;
        Bedrag = Bedrag - geld;
      } 

// snip

      geld = 0.05; 
      while (Bedrag >= geld) {
        Nr5c++;
        Bedrag = Bedrag - geld;
      } 
      geld = 0.01;
      while (Bedrag >= 0) {
        Nr1c++;
        Bedrag = Bedrag - geld;
      }
      Bedrag = 0;


    }/*ClsMoney*/
  }/*ClsMoney*/
}/*ConsAppl1TI33*/

Open in new window


/gustav
0
 
Dale BurrellDirectorCommented:
Looks OK - whats the issue?
0
 
lluddenCommented:
Something like this might work:


private string GetCurrency(double Amount)
{
      if (Amount == 0) {
            return "";
      }
      if (Amount / 100 > 1) {
            return string.Format("{0}x100{1}{2}", Convert.ToInt32(Amount / 100), Constants.vbCrLf, GetCurrency(Amount % 100));
      } else if (Amount / 10 > 1) {
            return string.Format("{0}x10{1}{2}", Convert.ToInt32(Amount / 10), Constants.vbCrLf, GetCurrency(Amount % 10));
      } else if (Amount > 1) {
            return string.Format("{0}x1{1}{2}", Convert.ToInt32(Amount), Constants.vbCrLf, GetCurrency(Amount % 1));
      } else if (Amount > 0.19) {
            return string.Format("{0}x20 cent{1}{2}", Convert.ToInt32(Amount / 0.19), Constants.vbCrLf, GetCurrency(Amount % 0.19));
      } else {
            return Convert.ToInt32(Amount * 100).ToString() + "x01";
      }
}
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
rd707Commented:
Sounds like a special case of this problem:

http://projecteuler.net/problem=31

You want loops for each amount of each coin that will satisfy the answer and store the number of coins.

Taking the above link as an example:

200 pence would require these loops:

0-1 x 200 pence
0-2 x 100 pence
0-4 x 50 pence
.
.
.
0-200 1 pence

You then just need to output all permutations that have the minimum coin quantity.
0
 
kensy11Author Commented:
Thanks , now i have something like this , but i get this error when i try to do this line of code
while (Bedrag >= 0.50) {

          Nr50c = Convert.ToUInt32(Math.Floor(Bedrag / 0.50));
          Bedrag = Bedrag % 0.50;
        }

Open in new window


error message:
Error	1	Operator '>=' cannot be applied to operands of type 'decimal' and 'double'

Open in new window


Error	2	Operator '/' cannot be applied to operands of type 'decimal' and 'double'

Open in new window


Error	3	Operator '%' cannot be applied to operands of type 'decimal' and 'double'

Open in new window







using System;

namespace ConsAppl1TI34 {
  class ConsCode {
      public void ExecuteProgram() {
      decimal Bedrag;
      uint Nr500, Nr200, Nr100, Nr50, Nr20, Nr10, Nr5, Nr2, Nr1, Nr50c, Nr20c, Nr10c, Nr5c, Nr2c, Nr1c;
      ClsMoney Cm = new ClsMoney();
      Console.Title = "Opgave 10: Splitsing geldbedrag (zonder tabel)";
      Bedrag = GiveAmount();
      Cm.SplitAmount(Bedrag, out Nr500, out Nr200, out Nr100, out Nr50, out Nr20, out Nr10, out Nr5,
      out Nr2, out Nr1, out Nr50c, out Nr20c, out Nr10c, out Nr5c, out Nr2c, out Nr1c);
      DisplayResult(Bedrag, Nr500, Nr200, Nr100, Nr50, Nr20, Nr10, Nr5,
      Nr2, Nr1, Nr50c, Nr20c, Nr10c, Nr5c, Nr2c, Nr1c);
      }/*ExecuteProgram*/

      public void DisplayResult(decimal Bedrag, uint Nr500, uint Nr200, uint Nr100, uint Nr50, uint Nr20, uint Nr10, uint Nr5, uint Nr2, uint Nr1, uint Nr50c, uint Nr20c, uint Nr10c, uint Nr5c, uint Nr2c, uint Nr1c) {
        Console.WriteLine("Een bedrag van {} Euro wordt het kortst verdeeld als:");


        Console.WriteLine("{0} X 500", Nr500);
        Console.WriteLine("{0} X 200", Nr200);
        Console.WriteLine("{0} X 100", Nr100);
        Console.WriteLine("{0} X 50", Nr50);
        Console.WriteLine("{0} X 20", Nr20);
        Console.WriteLine("{0} X 10", Nr10);
        Console.WriteLine("{0} X 5", Nr5);
        Console.WriteLine("{0} X 2", Nr2);
        Console.WriteLine("{0} X 1", Nr1);
        Console.WriteLine("{0} X 0,50", Nr50c);
        Console.WriteLine("{0} X 0,20", Nr20c);
        Console.WriteLine("{0} X 0,10", Nr10c);
        Console.WriteLine("{0} X 0,05", Nr5c);
        Console.WriteLine("{0} X 0,02", Nr2c);
        Console.WriteLine("{0} X 0,01", Nr1c);

      }
      public decimal GiveAmount()
      {
 	      Console.WriteLine("Geef een bedrag in euro in a.u.b ...");
        decimal GiveAmount = decimal.Parse(Console.ReadLine());
        return GiveAmount;
      }/*GiveAmount*/
  }/*ConsCode*/
}/*ConsAppl1TI34*/

Open in new window


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

namespace ConsAppl1TI34 {
  class ClsMoney {

    public void SplitAmount(decimal Bedrag, out uint Nr500, out uint Nr200, out uint Nr100, out uint Nr50, out uint Nr20, out uint Nr10, out uint Nr5,
      out uint Nr2, out uint Nr1, out uint Nr50c, out uint Nr20c, out uint Nr10c, out uint Nr5c, out uint Nr2c, out uint Nr1c){
      
      Nr500 = 0;
      Nr200 = 0;
      Nr100 = 0;
      Nr50 = 0;
      Nr20 = 0;
      Nr10 = 0;
      Nr5 = 0;
      Nr2 = 0;
      Nr1 = 0;
      Nr50c = 0;
      Nr20c = 0;
      Nr10c = 0;
      Nr5c = 0;
      Nr2c = 0;
      Nr1c = 0;

        while (Bedrag > 500) {

          Nr500 = Convert.ToUInt32(Math.Floor(Bedrag / 500));
          Bedrag = Bedrag % 500;
        }

        while (Bedrag >= 200) {

          Nr200 = Convert.ToUInt32(Math.Floor(Bedrag / 200));
          Bedrag = Bedrag % 200;
        }

        while (Bedrag >= 100) {

          Nr100 = Convert.ToUInt32(Math.Floor(Bedrag / 100));
          Bedrag = Bedrag % 100;
        }

        while (Bedrag >= 50) {

          Nr50 = Convert.ToUInt32(Math.Floor(Bedrag / 50));
          Bedrag = Bedrag % 50;
        }

        while (Bedrag >= 20) {

          Nr20 = Convert.ToUInt32(Math.Floor(Bedrag / 20));
          Bedrag = Bedrag % 20;
        }
        while (Bedrag >= 10) {

          Nr10 = Convert.ToUInt32(Math.Floor(Bedrag / 10));
          Bedrag = Bedrag % 10;
        }
        while (Bedrag >= 5) {

          Nr5 = Convert.ToUInt32(Math.Floor(Bedrag / 5));
          Bedrag = Bedrag % 5;
        }
        while (Bedrag >= 2) {

          Nr2 = Convert.ToUInt32(Math.Floor(Bedrag / 2));
          Bedrag = Bedrag % 2;
        } while (Bedrag >= 1) {

          Nr1 = Convert.ToUInt32(Math.Floor(Bedrag / 1));
          Bedrag = Bedrag % 1;
        } while (Bedrag >= 1) {

          Nr1 = Convert.ToUInt32(Math.Floor(Bedrag / 1));
          Bedrag = Bedrag % 1;
        }
        while (Bedrag >= 0.50) {

          Nr50c = Convert.ToUInt32(Math.Floor(Bedrag / 0.50));
          Bedrag = Bedrag % 0.50;
        }
        



    }



  }
}

Open in new window

0
 
alagurjanConnect With a Mentor Technology LeadCommented:
This will solve your problem


public CoinChangeAnswer findOptimalChange(int target, int[] denoms) {
     CoinChangeAnswer soln = new CoinChangeAnswer(target,denoms);
     StringBuilder sb = new StringBuilder();
       
     // initialize the solution structure
     for(int i=0; i<soln.OPT[0].length ; i++) {
         soln.OPT[0][i] = i;
         soln.optimalChange[0][i] = sb.toString();
         sb.append(denoms[0]+" ");
     }

     // Read through the following for more details on the explanation
     // of the algorithm.
     // http://condor.depaul.edu/~rjohnson/algorithm/coins.pdf
     for(int i=1 ; i<denoms.length ; i++) {
         for(int j=0; j<target+1 ; j++) {
          int value = j;
          int targetWithPrevDenomiation = soln.OPT[i-1][j];
          int ix = (value) - denoms[i];
          if( ix>=0 && (denoms[i] <= value )) {
              int x2 = denoms[i] + soln.OPT[i][ix];
              if(x2 <= target && (1+soln.OPT[i][ix] < targetWithPrevDenomiation)) {
               String temp = soln.optimalChange[i][ix] + denoms[i] + " ";
               soln.optimalChange[i][j] = temp;
               soln.OPT[i][j] = 1 + soln.OPT[i][ix];
              } else {
               soln.optimalChange[i][j] = soln.optimalChange[i-1][j]+ " ";
               soln.OPT[i][j] = targetWithPrevDenomiation;
              }
          } else {
              soln.optimalChange[i][j] = soln.optimalChange[i-1][j];
              soln.OPT[i][j] = targetWithPrevDenomiation;
          }
        }
     }
     return soln;
  }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.