Skip to content

Instantly share code, notes, and snippets.

@XDRosenheim
Last active March 21, 2018 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XDRosenheim/f5ccd17a5103c76decda8f9369c75c23 to your computer and use it in GitHub Desktop.
Save XDRosenheim/f5ccd17a5103c76decda8f9369c75c23 to your computer and use it in GitHub Desktop.
# Calculating a loan
def function(p, ir, d):
p = float(p)
d = int(d * 12)
if ir == 0:
return p / d
else:
ir = float((ir / 100) / 12)
temp1 = ir * ( 1 + ir ) ** d
temp2 = ( ( 1 + ir ) ** d) - 1
temp3 = temp1 / temp2
return p * temp3
# Calculating the remaining balance
# of a loan
def functionn(p, ir, d, n):
p = float(p)
d = int(d * 12)
n = int(n)
if ir == 0:
return p * (1 - (n / d))
else:
ir = float((ir / 100) / 12)
temp1 = float(((1+ir)**d) - ((1+ir)**n))
temp2 = float(((1+ir)**d) - 1)
return p * (temp1 / temp2)
## Main
# Loan amount
p = float(input("Enter loan amount: "))
# Interest rate
ir = float(input("Enter annual interest rate (percent): "))
# Duration
d = int(input("Enter loan duration in years: "))
print("LOAN AMOUNT:", int(p), "INTEREST RATE (PERCENT):", int(ir))
print("DURATION (YEARS):", int(d), "MONTHLY PAYMENT", int(function(p, ir, d)))
paid = 0
year = 0
while int(functionn(p, ir, d, year * 12)) > 0:
year += 1
for i in range(1,13):
paid += function(p, ir, d)
print("YEAR:", year,
"BALANCE:", int(functionn(p, ir, d, year * 12)),
"TOTAL PAYMENT:", int(paid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment