How to calculate compound interest in VB.NET

1 Answer

0 votes
Imports System

Class CompoundInterestCalculator
    Private Shared Function CalculateCompoundInterest(ByVal principal As Double, ByVal rate As Double, ByVal years As Double) As Double
        If principal < 0 OrElse rate < 0 OrElse years < 0 Then
            Console.WriteLine("Error: Principal, rate, and years must be non-negative values.")
            Return -1
        End If
		
		' Calculate total amount using the compound interest formula
        Dim amount As Double = principal * Math.Pow(1 + rate / 100, years)
		
		' Return compound interest
        Return amount - principal
    End Function

    Public Shared Sub Main()
        Dim principal As Double = 100000
        Dim rate As Double = 3.5
        Dim years As Double = 5
        Dim interest As Double = CalculateCompoundInterest(principal, rate, years)

        If interest >= 0 Then
            Console.WriteLine($"Principal Amount: {principal}")
            Console.WriteLine($"Annual Interest Rate: {rate}%")
            Console.WriteLine($"Years: {years}")
            Console.WriteLine($"Compound Interest: {interest}")
            Console.WriteLine($"Total Amount: {(principal + interest)}")
        End If
    End Sub
End Class



' run:
'
' Principal Amount: 100000
' Annual Interest Rate: 3.5%
' Years: 5
' Compound Interest: 18768.630564687453
' Total Amount: 118768.63056468745
' 

 



answered Aug 30, 2025 by avibootz

Related questions

...