Skip to content Skip to sidebar Skip to footer

Code To Calculate Minimum Monthly Credit Card Repayment Over A Year

Please I am trying to find out what is wrong with my reasoning, and hence my results. I am studying an online course where I am supposed to calculate the minimum figure needed to e

Solution 1:

You're almost there. There are a few problems in your implementation.

First off, you need to reset the balance after realizing the previously tested monthly payment didn't, well, pay out.

Secondly, the tabbing where you check the balance and increase it is wrong. As it stands, you pay 10 more dollars a month each month, which if I'm understanding your question properly is not what you want. You want to increase the monthly payment after seeing the one 10 dollars less didn't pay it off in 12 months.

Just as another point, your else: break is unnecessary as it will break out of the while loop when it enters into the next iteration.

startBalance = int(input("what's the stating balance? "))
balance = startBalance
numMonths = 12

annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12.0
monthlyPayment = 10

while (balance > 0):
    balance = startBalance # reset the balance each iteration

    print('checking monthly payment of',monthlyPayment)
    for each in range(0, numMonths):
        balance = balance - monthlyPayment
        balance = balance + (monthlyInterestRate * balance)
        # print('at month',each,'the balance is',balance)

    # changed the indentation below
    if (balance > 0):
        monthlyPayment += 10

print('you should pay',monthlyPayment,'per month')

Solution 2:

  1. The balance payed should be the same for the entire year so the if inside the for each doesn't make sense. The if isn't needed.

  2. The balance needs to be reset to the starting value when trying out a new monthly payment.

I tried it with these changes and it matches your test cases.


Solution 3:

How about this:

annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12.0
monthlyPayment = 10
running = true;

while (running):

    currentBalance = balance

    for each in range(0, 12):
        currentBalance = currentBalance - monthlyPayment
        currentBalance = currentBalance + (monthlyInterestRate * currentBalance)
    if (currentBalance > 0):
        monthlyPayment += 10
    else:
        running = false

print monthlyPayment

What I essentially did was getting the if-condition out of the for-each, with a copy for balance used. while(running) essentially iterates over possible values for monthlyPayment.

(you could go with while(currentBalance > 0) if currentBalance is set earlier, but I'd go with my while(running) approach so it is read like a do-until-loop)


Solution 4:

Outstanding = 59400 # Total Outstanding Amount
interestrate = 4.2  # Interest Rate per month+ GST

#print("Month", "\t", "Interest+GST", "\t\t", "MinAmtDue", "\t\t\t", "Balance")
#print("-----", "\t\t", "-------", "\t\t", "--------", "\t\t\t", "-------")

month = 0
totpmt = 0
interest_GST = 0
minamtdue = 0
outstandingamt1 = Outstanding
while (outstandingamt1 + interest_GST - minamtdue) > 0 :
    month += 1
    interest_GST = outstandingamt1*4.2/100
    minamtdue = outstandingamt1 * 5/100
    #minamtdue = 12000
    outstandingamt1 = outstandingamt1 + interest_GST - minamtdue
    #print(month, "\t\t\t", round(interest_GST,2), "\t\t", round(minamtdue,2), "\t\t\t", round(outstandingamt1,2))
    totpmt = totpmt + minamtdue
#print(month+1, "\t\t\t", 0, "\t\t", round(outstandingamt1,2), "\t\t\t", 0)
print("Total Amount to be paid in ", month+1, "months= ", round(totpmt+outstandingamt1 , 2))

Post a Comment for "Code To Calculate Minimum Monthly Credit Card Repayment Over A Year"