qbasic

Calculating Simple Interest and output into tabular form


I'm trying to write a QBASIC Code that allow a user to input Principal and Time of 5 Investors to Calculates the Simple Interest of those 5 Investors and Output their, Amount Accrued, Principal and Time/Duration in a Tabular form. All with an Interest Rate of 12%

I try:

DIM P AS DOUBLE
DIM r AS DOUBLE
DIM t AS DOUBLE
DIM A AS DOUBLE
DIM rt AS DOUBLE
CLS
DIM x AS INTEGER
CLS
WHILE x <> 5
    INPUT "Enter First Investor's Principal: ", P
    INPUT "Enter First Investor's Time: ", t
    x = x + 1

WEND
r = 0.12
rt = 1 + (r * t)
A = P * rt
FOR i = 1 TO x
    PRINT "Amount Accrued", "Principal Amount", "Time"
    PRINT
    PRINT A, , P, , t
NEXT i

But it keep printing the last result 5 times in 5 different tables

I'm Expecting results like below:

Amount Accrued    Principal   Time/Duration
44,400            30,000      4
68,000            50,000      3
95,200            70,000      5

Solution

  • This is happening because you're overwriting P and t in every iteration of your WHILE loop. Initialise P and t and other subsequent variables as arrays instead and store different values at different indices of them. Also note that you're always printing FirstInvestor's Principal and Time in each iteration. The investor number should iterate as well. I have fixed the problems in the code below:

    CLS
    DIM x AS INTEGER
    INPUT "Number of Investors: ", x
    DIM P(x - 1) AS DOUBLE
    DIM r AS DOUBLE
    DIM t(x - 1) AS DOUBLE
    DIM A(x - 1) AS DOUBLE
    DIM rt(x - 1) AS DOUBLE
    
    FOR i = 0 TO x - 1
        PRINT "Enter Investor-" + STR$(i + 1) + "'s Principal: "
        INPUT "", P(i)
        PRINT "Enter Investor-" + STR$(i + 1) + "'s Time: "
        INPUT "", t(i)
    NEXT i
    
    PRINT "Amount Accrued", "Principal Amount", "Time"
    PRINT
    
    r = .12
    FOR i = 0 TO x - 1
        rt(i) = 1 + (r * t(i))
        A(i) = (P(i) * rt(i))
        PRINT A(i), P(i), , t(i)
    NEXT i