So i am having troubles with this code. I get the dollars part to work correctly but the rest is not. Im trying to change 529 cents into 5 dollars 1 quarter 0 dimes 0 nickels 4 pennies, here is my code.
Option Explicit On
Option Strict On
Module Module1
Sub Main()
Dim amount As Integer
Dim dollars As Integer
Dim quarters As Integer
Dim dimes As Integer
Dim nickels As Integer
Dim pennies As Integer
Console.WriteLine("Please Enter A Unit of Cents")
amount = CInt(Console.ReadLine())
If (amount >= 100) Then
dollars = amount \ 100
amount = amount - (100 * dollars)
ElseIf (amount >= 25) Then
quarters = amount \ quarters
amount = amount - (25 * quarters)
ElseIf (amount >= 10) Then
dimes = amount \ dimes
amount = amount - (10 * dimes)
ElseIf (amount >= 5) Then
nickels = amount \ nickels
amount = amount - (5 * nickels)
ElseIf (amount >= 1) Then
pennies = amount
End If
Console.WriteLine("" & dollars & " dollars " & quarters & " quarters " & dimes & " dimes " & nickels & " nickels " & pennies & " pennies ")
Console.ReadLine()
End Sub
End Module
Write a function to automate the counting of coins. You were almost there. Just use modulo to find the remaining pennies each time a count is done.
Sub Main()
Dim amount As Integer
Dim dollars As Integer
Dim quarters As Integer
Dim dimes As Integer
Dim nickels As Integer
Dim pennies As Integer
Console.WriteLine("Please Enter A Unit of Cents")
amount = CInt(Console.ReadLine())
dollars = parseDenomination(amount, 100)
quarters = parseDenomination(amount, 25)
dimes = parseDenomination(amount, 10)
nickels = parseDenomination(amount, 5)
pennies = amount
Console.WriteLine("" & dollars & " dollars " & quarters & " quarters " & dimes & " dimes " & nickels & " nickels " & pennies & " pennies ")
Console.WriteLine($"{getAmountString(dollars, "Dollar")}, {getAmountString(quarters, "Quarter")}, {getAmountString(dimes, "Dime")}, {getAmountString(nickels, "Nickel")}, {getAmountString(pennies, "Penny")},")
Console.ReadLine()
End Sub
Private Function parseDenomination(ByRef amount As Integer, count As Integer) As Integer
Dim result = amount \ count
amount = amount Mod count
Return result
End Function
Private Function getAmountString(amount As Integer, name As String) As String
Return $"{name}{If(amount = 1, "", "s")}: {amount}"
End Function
I added an additional method to help get the string output. It kind of breaks down for "pennies / pennys" though - that's English for you.
Related
After running through the following program, lownum stays 0. It makes complete sense if you do not enter any negative numbers. However, what are my options to get the lowest number if all positive numbers are entered.
Sub Main()
Declaring variables
Dim number1 As Integer = 0
Dim number2 As Integer = 0
Dim lownum As Integer
Dim highnum As Integer
'For statement to run loop 10 times
For counter As Integer = 1 To 10
'Prompting user to enter two numbers
Console.Write("Enter the first number: ")
number1 = Console.ReadLine
Console.Write("Enter the second number: ")
number2 = Console.ReadLine
'If statements to determine and keep track of highest and lowest number
If number1 > number2 Then
Console.WriteLine("Number 1 is larger " & number1)
ElseIf number2 > number1 Then
Console.WriteLine("Number 2 is larger " & number2)
Else
Console.WriteLine("The two numbers are equal: " & number1 & " " & number2)
End If
If number1 > highnum Then
highnum = number1
End If
If number1 < lownum Then
lownum = number1
End If
If number2 > highnum Then
highnum = number2
End If
If number2 < lownum Then
lownum = number2
End If
Next
'Displaying highest and lowest numbers
Console.WriteLine("The highest number entered was " & highnum)
Console.WriteLine("The lowest number entered was " & lownum)
'Prompting user for input to continue
Console.WriteLine("Press any key to continue: ")
Console.ReadKey()
End Sub
Just check your counter variable. If it is 1, then set low and high to your values appropriately. If it is greater than 1, then do the comparisons:
For counter As Integer = 1 To 10
' ... other code ...
If counter = 1 Then
lownum = Math.Min(number1, number2)
highnum = Math.Max(number1, number2)
Else
lownum = Math.Min(lownum, Math.Min(number1, number2))
highnum = Math.Max(highnum, Math.Max(number1, number2))
End If
Next
You testing for values being less or greater then your two starting variables of 0.
If you don't give the min/max values and don't enter values < 0 or > 0 then the values of Min/Max are NEVER set.
So you want to set these to the max allowable and min allowable values.
Change you variable declares to this with initialization of the min/max and your code should work just fine.
Dim number1 As Integer = 0
Dim number2 As Integer = 0
Dim lownum As Integer = Integer.MaxValue
Dim highnum As Integer = Integer.MinValue
I'm trying to get the integers of "num3"(input from the user) and squaring each character and eventually see if its a "happy number"=1 or a "unhappy number" includes 4
For example, 19 is happy number 1^2+9^2=82 , 8^2+2^2=68,6^2+8^2=100, 1^2+0+0=1
Sub Main()
Dim number, num2, total As Integer
Dim num3 As String
Console.Write("pick a number:")
number = Console.ReadLine()
num2 = 0
num3 = Convert.ToString(number)
Do Until total = 1 Or num2 = 4
For Each num3 In num3.Substring(0, num3.Length)
For counter = 1 To num3.Length And num3 = num3.Substring(0, num3.Length)
num2 = num3 ^ 2
total = total + num2
Console.WriteLine(total)
Next
num3 = total
Next
Loop
Console.ReadLine()
If total = 1 Then
Console.WriteLine("happy number")
ElseIf num2 = 4 Then
Console.WriteLine(number & "is a unhappy number")
Console.ReadLine()
End If
End Sub
End Module
however i'm stuck whether "num3" replace itself from the for loop
Here, I believe this should work:
Dim InputNum As Integer = 45 'Your input number
Dim TempNum As Integer = InputNum 'Here we hold number temporarily during steps 19>82....
Do
Dim SumTotal As Double = 0 'sum of numbers squares
For Each number As Char In TempNum.ToString
SumTotal += Integer.Parse(number) ^ 2
Next
TempNum = CInt(SumTotal)
Debug.Print(TempNum.ToString)
'If result is 4 it will loop forever
If SumTotal = 4 Then
Console.WriteLine(InputNum & " is an unhappy number")
Exit Do
ElseIf SumTotal = 1 Then
Console.WriteLine(InputNum & " is a happy number")
Exit Do
End If
Loop
I have created a code to work out pay of workers, and to - 1 of pay for charity if it is over 100, and every 10 over 100 -1. But I need to get rid of the decimal with new pay variable. At the end of the if function
Dim donation As Single
Dim newpay As Single
Dim hourlyRate As Decimal
Dim pay As Decimal
Dim hours As Single
Sub Main()
Console.WriteLine("how many hours have you worked this week?")
hours = Console.ReadLine
Console.WriteLine("what is your hourly pay?")
hourlyRate = Console.ReadLine
pay = hours * hourlyRate
newpay = pay
If pay > 100 Then
pay = pay - 1
newpay = newpay - 100
donation = donation + 1
newpay = newpay / 10
Math.Floor(newpay)
pay = pay - newpay
donation = donation + newpay
End If
Console.WriteLine("you earned £" & pay & " and donated £" & donation & " to charity")
Console.ReadLine()
End Sub
Use the Math.Floor function to round down
SO if newpay was 3.6
Math.Floor(newpay)
will return 3
SO
Use the result of this function
donation = donation + Math.Floor(newpay)
OR
newpay = Math.Floor(newpay)
I am having issues trying to make a calculator that accurately calculates deposits plus the amount of interest that is added monthly. I dont know how to incorporate a loop that can add monthly deposits to total deposits, then adds the total deposits plus total interest times the interest rate / 100 / 12 to the total deposits. Here is what I have so far. Sorry am super new to VB !
Dim intMonthlyDeposit As Interger
Dim intMonthsTotal As Integer
Dim intAnnualRate As Interget
Dim decTotalDeposits As Decimal
Dim decTotalInterest As Decimal
Dim decTotalTotal As Decimal
Try
intMonthlyDeposit = CInt(txtMonthlyDeposits.Text)
intMonthsTotal = CInt(txtMonths.Text)
decAnnualRate = CDec(txtAnnualRate.Text)
decTotalDeposits = 0
decTotalInterest = 0
decTotalTotal = 0
decTotalDeposits = decMonthlyDeposit * intMonthsTotal
decTotalInterest = decTotalDeposits * (decAnnualRate / 100)
decTotalTotal = decTotalDeposits + decTotalInterest
lblTotDeposit.Text = decTotalDeposits.ToString("C")
lblTot.Text = decTotalTotal.ToString("C")
lblTotInterest.Text = decTotalInterest.ToString("C")
Catch ex As Exception
MsgBox("Enter a Valid Number")
End Try
End Sub
This is how I implemented the loop for calculating total interest and total deposits correctly.
For index As Integer = 1 To intMonthsTotal Step 1
decTotalDeposits += decMonthlyDeposit
decTotalInterest += (decTotalDeposits + decTotalInterest) * ((decAnnualRate / 100) / 12)
Ok I have got this far and if you run it, it will do what you ask. Now when I type in 99999 or -99999 it will not end. Can someone tell me what I am doing wrong. I am suppose to loop until a sentinel value of -99999 is enter for previous meter reading.
Sub Main()
' program to compute a consumer’s electric bill. It will calculate the bill for one or more customers
' by looping until a sentinel value of -99999 is entered for the previous meter reading.
Dim previousReading As Integer = 0
Dim currentReading As Integer = 0
Do While (previousReading <> -99999)
Dim salesTax As Double
' prompt user to input value for previous reading then convert to integer
Console.WriteLine("Enter the value of previous meter reading")
previousReading = Convert.ToInt32(Console.ReadLine())
' prompt user to input value for current reading then convert to integer
Console.WriteLine("Enter the value of current meter reading")
currentReading = Convert.ToInt32(Console.ReadLine())
Dim kwhConsumed As Integer
Dim electricCharge, totalBill As Double
' calculate KWH consumed
kwhConsumed = currentReading - previousReading
' Use select case to determine electricCharge
Select Case kwhConsumed
Case Is < 500
electricCharge = kwhConsumed * 0.05
Case 500 To 1000
electricCharge = 25 + ((kwhConsumed - 500) * 0.055)
Case Is > 1000
electricCharge = 52.5 + ((kwhConsumed - 1000) * 0.06)
End Select
' calculate sales tax
salesTax = electricCharge * 0.085
' calculate total charges
totalBill = electricCharge + salesTax
' Output values for kwhConsumed, electricCharge, salesTax, and totalBill
Console.WriteLine("KWH consumed = " & kwhConsumed & " KWH")
Console.WriteLine("Electric charge = $" & Math.Round(electricCharge, 2))
Console.WriteLine("Sales tax = $" & Math.Round(salesTax, 2))
Console.WriteLine("Total bill = $" & Math.Round(totalBill, 2))
Loop
End Sub
You can try using string comparison instead for previousReading <> -99999. You also need to use absolute value to consider both -99999 and 99999. Do something like this
Do While (previousReading <> 99999)
//code
previousReading = Math.Abs(Convert.ToInt32(Console.ReadLine()))
//code
Loop
I'm guessing this is homework?
Instead of blurting out the answer, I wonder if you might think about inserting a Debug.Print statement and some kind of "break" statement after your previousReading = Convert.ToInt32 statement. To look for the "break" statement, search for "vb.net exit loop" and see what pops up.