' DOUBLE Financial Calculator — interest, amortization table
#COMPILE EXE
#DIM ALL

FUNCTION CompoundInterest(BYVAL principal AS DOUBLE, BYVAL rate AS DOUBLE, BYVAL periods AS LONG) AS DOUBLE
    LOCAL i AS LONG
    LOCAL result AS DOUBLE
    result = principal
    FOR i = 1 TO periods
        result = result + result * rate
    NEXT i
    FUNCTION = result
END FUNCTION

SUB AmortizationTable(BYVAL loan AS DOUBLE, BYVAL rate AS DOUBLE, BYVAL months AS LONG)
    LOCAL i AS LONG
    LOCAL payment AS DOUBLE
    LOCAL balance AS DOUBLE
    LOCAL interest AS DOUBLE
    LOCAL principal AS DOUBLE
    LOCAL monthlyRate AS DOUBLE
    LOCAL temp AS DOUBLE
    LOCAL waitStr AS STRING
    monthlyRate = rate / 12.0
    temp = 1.0
    FOR i = 1 TO months
        temp = temp * (1.0 + monthlyRate)
    NEXT i
    payment = loan * monthlyRate * temp / (temp - 1.0)
    PRINT "=== AMORTIZATION TABLE ==="
    PRINT "Loan: $" & STR$(loan)
    PRINT "Annual Rate: " & STR$(rate * 100) & "%"
    PRINT "Monthly Payment: $" & STR$(payment)
    PRINT "Term: " & STR$(months) & " months"
    PRINT STRING$(60, "-")
    PRINT "Month  Payment   Interest  Principal  Balance"
    PRINT STRING$(60, "-")
    balance = loan
    FOR i = 1 TO months
        interest = balance * monthlyRate
        principal = payment - interest
        IF principal < 0 THEN principal = 0
        balance = balance - principal
        IF balance < 0 THEN balance = 0
        PRINT STR$(i) & "   " & STR$(payment) & "   " & STR$(interest) & "    " & STR$(principal) & "  " & STR$(balance)
        IF i MOD 12 = 0 THEN
            INPUT "", waitStr
        END IF
    NEXT i
    PRINT "Loan paid off!"
    PRINT
END SUB

SUB SimpleInterest(BYVAL principal AS DOUBLE, BYVAL rate AS DOUBLE, BYVAL years AS LONG)
    LOCAL interest AS DOUBLE
    LOCAL total AS DOUBLE
    interest = principal * rate * years
    total = principal + interest
    PRINT "=== SIMPLE INTEREST ==="
    PRINT "Principal: $" & STR$(principal)
    PRINT "Rate: " & STR$(rate * 100) & "%"
    PRINT "Years: " & STR$(years)
    PRINT "Interest: $" & STR$(interest)
    PRINT "Total: $" & STR$(total)
    PRINT
END SUB

SUB SavingsForecast()
    LOCAL deposit AS DOUBLE
    LOCAL rate AS DOUBLE
    LOCAL years AS LONG
    LOCAL i AS LONG
    LOCAL balance AS DOUBLE
    deposit = 500.0
    rate = 0.05
    years = 30
    balance = 0
    PRINT "=== SAVINGS FORECAST ==="
    PRINT "Monthly deposit: $" & STR$(deposit)
    PRINT "Annual rate: " & STR$(rate * 100) & "%"
    PRINT "Period: " & STR$(years) & " years"
    PRINT STRING$(50, "-")
    PRINT "Year   Deposits   Interest    Balance"
    PRINT STRING$(50, "-")
    FOR i = 1 TO years
        balance = balance + deposit * 12
        balance = balance + balance * rate
        PRINT STR$(i) & "    " & STR$(deposit * 12 * i) & "  " & STR$(balance - deposit * 12 * i) & "  " & STR$(balance)
    NEXT i
    PRINT STRING$(50, "-")
    PRINT
END SUB

SUB CurrencyPrecisionDemo()
    LOCAL a AS DOUBLE
    LOCAL b AS DOUBLE
    LOCAL c AS DOUBLE
    a = 1.23456789
    b = 9.87654321
    c = a + b
    PRINT "=== DOUBLE PRECISION ==="
    PRINT "a = " & STR$(a)
    PRINT "b = " & STR$(b)
    PRINT "a + b = " & STR$(c)
    PRINT "a * b = " & STR$(a * b)
    PRINT "b / a = " & STR$(b / a)
    PRINT "8 = " & STR$(4)
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    LOCAL loanAmt AS DOUBLE
    LOCAL annualRate AS DOUBLE
    LOCAL months AS LONG
    PRINT "==============================="
    PRINT " DOUBLE Financial Calculator"
    PRINT "==============================="
    PRINT
    CALL SimpleInterest(10000.0, 0.05, 3)
    CALL CurrencyPrecisionDemo()
    CALL SavingsForecast()
    PRINT "Amortization Table Calculator"
    INPUT "Loan amount: ", loanAmt
    PRINT "Compound interest on $1000 at 5% for 10 years:"
    PRINT "$" & STR$(CompoundInterest(1000.0, 0.05, 10))
    PRINT
    PRINT "Finance calculations complete."
    FUNCTION = 0
END FUNCTION
