Question:
need help programing code in visual basics?
xova
2012-09-22 21:08:25 UTC
I need to make a program when you enter an amount like 93 you will get an output like, 3 quarters. 1 dime, 1 nickel and 3 cents this is what i have so far but i do not know how to write the rest of the code.. my listboxes are labled as such.
txtamount, lstquarters, lstdimes, lstnickels, lstcents.

Public Class frmchange

Private Sub btncomp_Click(sender As System.Object, e As System.EventArgs) Handles btncomp.Click
' this is where i list all the var's are listed
Dim quarters, nickles, dimes, cents As Integer, change As Double
change = txtamount.Text
quarters = 0.25
dimes = 0.1
nickles = 0.5
cents = 0.01

End Sub
End Class
Three answers:
?
2012-09-22 23:32:42 UTC
Ok so, let's start with the errors you already have



First you said: Dim quarters, nickles, dimes, cents As Integer

Then you said:

quarters = 0.25

dimes = 0.1

nickles = 0.5

cents = 0.01



Integer type = whole number. No decimals. So either change the data type or change the data.



You're going to need another set of variables to store how many of each currency type you'll need.



Dim myQuarters, myDimes, myNickles, myCents as Integer

myQuarters = 0

myDimes = 0

myNickles = 0

myCents = 0

While change >= quarters

myQuarters += 1

change -= quarters

End While

'

' I don't know why you're using listbox controls when a lable will do.

myQuartersLabel.Text = String.Format("{0} Quarters", myQuarters)

'

' Do this for remaining money types





There are better and more efficient ways to do this but if you turn in that code your instructor is going to know you had someone else do your homework for you.
Mr Ed
2012-09-22 23:46:30 UTC
You might be biting off more than you can chew. This program might be above your current skill level.



There are actually several ways to program this "logic." Here is one way that might be easiest for you:



You need to first divide your number, .93, in this case, by .25. This will tell you how many quarters you need. .93/.25 = 3.72. You take the number before the decimal point. So now you know you need THREE quarters.



Then you take the number of quarters you have, 3, multiply that by .25, and subtract from your .93. This gives you 18. THAT's what you have left, so you divide THAT by .10. The answer is 1.8 so you now know you need ONE dime. Again, take the number before the decimal point.



Then you take the number of dimes you have, 1, multiply that by .10, and subtract that from the 18. This gives you what you have left, which is now .08 and you divide THAT by .05. The answer is 1.6 so you now know you need ONE nickel.



Then you take the number of nickels you have, 1, multiply that by .05, and subtract that from the 08. This gives you .03, and you divide that by .03... which is 3.00... so you know you need THREE pennies.



Here's another example: 1.27



Divided by .25 = 5.08... 5 quarters

5 x .25 = 125 from 127 = .02 is what we have left to work with.



.02 divided by .10 = 0.02... 0 dimes

.02 Divided by .05 = 0.04... 0 nickels

.02 Divided by .01 = 2.00... 2 pennies



So if the original number is 1.27, the output will be 5 quarters, 0 dimes, 0 nickels, and 2 pennies.



You can put this all in one subroutine, or you can have four separate subs, one for each coin. That makes no difference. You call the routine once each, in order, to determine the number of coins.



It's not hard, but it sounds like this might be above your current skill level. I don't program in Visual Basic (PowerBASIC for me), but it would only take me just a few minutes to program this in PowerBASIC.
?
2016-12-18 12:57:32 UTC
hi, There are some issues you're able to desire to understand: First, all the .internet languages(c#, vb, etc) are going to be very comparable because of the fact they use the comparable library's(.internet framework) you basically extremely need to understand certainly one of them to jot down code in the different; the transition from one .internet language to a various is basically syntax. subsequent, the common public of programming languages share the comparable form of systems, archives varieties, and good judgment. the only puzzling section approximately learning to code is the suggestions; as quickly as you have them down that's somewhat ordinary to examine a language. i want to recommend you keep learning with seen hassle-loose; the ultimate way is formal coaching - %. up some night instructions, they are going to do you a international of stable.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...