1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Problem with simple program to compute change

Discussion in 'Meet and Greet' started by dhess62, Jun 16, 2009.

  1. dhess62

    dhess62 New Member

    This program takes a number of pennies and converts it to dollars, quarters, dimes, nickels and pennies, here is the code that I wrote. The problem is, for some reason it always throws in a nickel when it computes the change for any amount that has a penny in it. Any ideas as to why?

    example if you give it 346 pennies will return:
    3 Dollars
    1 Quarter
    2 Dimes
    1 Nickel <<<there's the problem
    1 Penny

    Code:
     Private Sub btnCalculateChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateChange.Click
            txtNumOfPennies = Convert.ToInt32(Me.txtNumOfPennies.Text)
            Me.txtDollar.Text = Int((txtNumOfPennies.Text) / 100)
            Me.txtQuarter.Text = Int(((txtNumOfPennies.Text) Mod 100) / 25)
            Me.txtDime.Text = Int((((txtNumOfPennies.Text) Mod 100) Mod 25) / 10)
            Me.txtNickel.Text = Int((((txtNumOfPennies.Text) Mod 100) Mod 25) Mod 10 / 5)
            Me.txtPenny.Text = Int(((((txtNumOfPennies.Text) Mod 100) Mod 25) Mod 10) Mod 5)
     
    Last edited by a moderator: Jun 17, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Hi, and Welcome to G4EF ! :)

    You have a flaw in the code. You miss a pair of brackets.
    Look at this line :

    Code:
    Me.txtNickel.Text = Int((((txtNumOfPennies.Text) Mod 100) Mod 25) Mod 10 / 5)
    As the division operator '/' has higher priority than Mod, The above code simplifies to :

    Code:
    Me.txtNickel.Text = Int((((txtNumOfPennies.Text) Mod 100) Mod 25) Mod 2)
    What you noticed :
    is correct. 1 Mod 2 = 1 Mod 5 = 1

    So to correct the problem, just change the above line to :

    Code:
    Me.txtNickel.Text = Int(((((txtNumOfPennies.Text) Mod 100) Mod 25) Mod 10) / 5)
     

Share This Page