VB program to calculate monthly deposits plus interest - vb.net

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)

Related

How to remove decimal from variable in vb.net

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)

Receive "infinity" as result in mortgage payment calculation in Visual Basic

I'm a first year programming student and am trying to make a mortgage payment calculator. Users enter their loan amount and interest rate, select 15 or 30 years for the term length, and their monthly payment should be calculated. I think the math is correct, but I think I'm running into problems with converting data types. The only result I'm able to calculate is "Infinity". I've also tried declaring all variables as Doubles, but no difference.
Below is the piece of code I'm having trouble with, and Option Strict On is included in the full code. Any tips would be greatly appreciated!
Dim decLoanAmount As Decimal
Dim decInterestRate As Decimal
Dim decFifteen As Decimal = 180D
Dim decThirty As Decimal = 360D
Dim decNumberOfMonths As Decimal
Dim dblPayment As Double
Dim decPayment As Decimal
' Did user enter a numeric value?
If IsNumeric(txtLoanAmount.Text) And IsNumeric(txtInterestRate.Text) Then
decLoanAmount = Convert.ToDecimal(txtLoanAmount.Text)
decInterestRate = Convert.ToDecimal(txtInterestRate.Text)
' Is Loan Amount greater than zero?
If decLoanAmount > 0 And decInterestRate > 0 Then
If radFifteen.Checked = True Then
decNumberOfMonths = decFifteen
ElseIf radThirty.Checked = True Then
decNumberOfMonths = decThirty
End If
' Calculate the monthly payments as a double
dblPayment = (decLoanAmount * (decInterestRate / 12 / 100) * (1 + (decInterestRate / 12 / 100) _
^ decNumberOfMonths)) / ((1 + (decInterestRate / 12 / 100) ^ decNumberOfMonths) - 1)
' Convert double to decimal
decPayment = Convert.ToDecimal(decPayment)
' Display monthly payment
lblPayment.Text = decPayment.ToString("C2")
Else
If decLoanAmount < 0 Then
MsgBox("Please enter a valid loan amount.", , "Input error")
End If
If decInterestRate < 0 Then
MsgBox("Please enter a valid interest rate.", , "Input error")
End If
End If
Else
' Display error message if user entered a negative value.
MsgBox("Please enter a numeric value.", , "Input Error")
End If
End Sub
Based on the Wikipedia formula, you aren't doing it correctly. It should look something like this:
dblPayment = (decLoanAmount * (decInterestRate / (12 * 100))) / (1 - (1 + (decInterestRate / (12 * 100))) ^ (-decNumberOfMonths))
Also, this line is weird
decPayment = Convert.ToDecimal(decPayment)
Should be
decPayment = Convert.ToDecimal(dblPayment)

How to code Auto Loan calc with fixed months and interest? (VB)

I need to make an auto loan calculator that is only able to calculate 2.3% interest for 84 months. I've never made a calculator that didn't allow a user to input these amounts, so how do I code that part of the calc?
Dim res As Decimal = 100 ' Current value
Dim interest As Decimal = 1.89 / 100 ' 1.89%
For i As Integer = 1 To 89 'Months
Dim amr As Decimal = res * interest
res += amr
Next
MsgBox(res)
Depends on what you want to calculate.. Monthly paiement, equity, what is left to pay ?
Here's the formula to calculate the monthly paiement of a loan
Dim monthlyPaiement As Decimal
Dim loanAmount As Decimal
Dim interestRate As Decimal
Dim loanLengthInMonth As Decimal
loanAmount = 25000
interestRate = 1.9 / 100
loanLengthInMonth = 84
interestRate /= 12 ' Divide by number of month in a year
monthlyPaiement = loanAmount * ((1 + interestRate) ^ loanLengthInMonth) * interestRate / (((1 + interestRate) ^ loanLengthInMonth) - 1)

Can't understand why my variable doesn't change

I'm building a little time > pay conversion program in VB. I'm really new to VB and don't understand why my variable pay doesn't calculate like it should. I plug in 5 5's as a test and get $0.
Dim total As Double = 0.0
Dim timeCounter As Integer = 0
Dim time As Integer = 0
Dim pay As Double = 0.0
While timeList.Items.Count < 5
time = timeList.Items(timeCounter)
total += time
timeCounter += 1
End While
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
Else
PayLabel.Text = "Error"
End If
End If
PayLabel.Text = "$" & pay
Your syntax should be something like this:
For intCount = 0 To timeList.Items.Count
time = timeList.Items(intCount)
total += time
Next intCount
This will avoid an infinite loop.
To fix your 40+ issue:
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
End If
Else
PayLabel.Text = "Error"
End If
this would be my fix into a console apps
for process will return $0, second $100
Module Module1
Sub Main()
Dim timeList As New List(Of Integer)
timeList.AddRange(New Integer() {1, 2, 3, 4, 5, 6})
process(timeList)
timeList.Clear()
timeList.AddRange(New Integer() {1, 2, 3, 4})
process(timeList)
Console.Read()
End Sub
Private Sub process(timeList As List(Of Integer))
Dim total As Double = 0.0
Dim timeCounter As Integer = 0
Dim time As Integer = 0
Dim pay As Double = 0.0
While timeList.Count < 5 AndAlso timeCounter < timeList.Count
time = timeList(timeCounter)
total += time
timeCounter += 1
End While
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
Else
Console.WriteLine("error")
End If
End If
Console.WriteLine("$" & pay)
End Sub
End Module
This could be better solved with a functional approach. To get the sum of the list of integers do the following:
Dim totalTime = timeList.Sum()
Then you can follow the logic you laid out. I would highly recommend learning to use Linq Set Functions to make your code your readable and easier to understand. Good Luck.

VB Simple If statements (h/w)

I don't deal with VB. I am helping my mother with her homework and just cant think straight anymore on what to do with this statement.
Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click
Dim intPackA As Integer = 0
Dim intPackB As Integer = 0
Dim intPackC As Integer = 0
Dim SavingsPriceB As Decimal = 0.0
Dim SavingsPriceC As Decimal = 0.0
Dim TotalPrice As Decimal = 0.0
lblTotal.Text = String.Empty
If radPackA.Checked Then
TotalPrice = 9.95
lblTotal.Text = TotalPrice
If Integer.TryParse(txtHours.Text, intPackA) Then
If intPackA > 10 Then
TotalPrice = TotalPrice + ((intPackA - 10) * 2)
lblTotal.Text = TotalPrice
End If
End If
If chkSavings.Checked Then
SavingsPriceB = 14.95 + ((intPackB - 20) * 1)
SavingsPriceC = 19.95
If TotalPrice < SavingsPriceB And TotalPrice < SavingsPriceC Then
lblTotal.Text = TotalPrice & ", no savings with Package B or C"
End If
End If
ElseIf radPackB.Checked Then
TotalPrice = 14.95
lblTotal.Text = TotalPrice
If Integer.TryParse(txtHours.Text, intPackB) Then
If intPackB > 20 Then
TotalPrice = TotalPrice + ((intPackB - 20) * 1)
lblTotal.Text = TotalPrice
End If
End If
If chkSavings.Checked Then
End If
ElseIf radPackC.Checked Then
TotalPrice = 19.95
lblTotal.Text = TotalPrice
End If
If chkNonprofit.Checked Then
TotalPrice = Format((TotalPrice - ((TotalPrice / 100) * 20)), ".00")
lblTotal.Text = TotalPrice & ControlChars.CrLf & "Non-Profit Organization discount applied"
End If
End Sub
It's the If chkSavings.Checked that's giving me problem.
This is the program as designed. There is a label bellow the packages that displays the total.
When the Potential Savings is checked, it should also display the amount you could save if you use a different package.
So if I put Package A, 5 hours, 20% discount it should say $7.96, no savings with Package B or C. For Package A, 25 hours it should say $39.95, save $20.00 with Package B, and save $20.00 with Package C
The code I have does not print it even the first part.
Package A and less then 10 hours = $9.95, every additional hour is $2.00 more
Package B and less then 20 hours = $14.95, every additional hour is $1.00 more
Package C with unlimited hours is = $19.95
So My question is, what am I doing wrong in my code or how could I achieve what I am looking for.