Link to home
Start Free TrialLog in
Avatar of kensy11
kensy11Flag for Belgium

asked on

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
Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

Looks OK - whats the issue?
Avatar of lludden
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";
      }
}
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.
Avatar of kensy11

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial