Calculate the interest rate

Mar 6, 2022
Reaction score
Hello, I have difficulty understanding part of the code of the following question:

You want to invest capital 0 for n years at an annual interest rate of i%.
The relation is given by: = 0*(1 + )^.
Write a program giving you the amount earned, the interest ( = − 0) of your
investment, as well as the equivalent monthly interest rate under the database
provided. = (1 + )^(1/12) − 1.

The following would be the answer:

from math import *

C0 = float(input(‘Starting capital: ‘))
i = float(input("Annual interest rate: "))
i = i / 100
u = 1 + i
n = int(input("Number of years: "))

C = C0 * (un)
I = C - C0
im = u
(1/12) - 1

C = int(100 * C)/100
I = int(100 * I)/100
im = int(10000 * im)/100

print(‘Capital after’, n, ‘years:’, C, ‘€’)
print('Interest: ', I, ‘€’)
print("Monthly interest rate: ", im, ‘%’)

The code I don’t understand is the following:

I do not understand why this was written, what it means and how it affects the code:

C = int(100 * C)/100
I = int(100 * I)/100
im = int(10000 * im)/100

Many thanks for who can help me out!
Apr 30, 2023
Reaction score
These lines of code are used to round the values of C, I, and im to two decimal places for better readability.

The int() function is used to convert a floating point number to an integer by truncating the fractional part. For example, int(3.14) returns 3.

Multiplying by 100 first allows us to shift the decimal point two digits to the right, effectively multiplying by 100 without changing the value. We then take the integer value of this expression, which removes any digits after the second decimal place. Finally, we divide by 100 again to shift the decimal point back to its original position.

For example, if C has the value 1234.5678, then int(100 * C)/100 will first evaluate to 123456.78. Taking the integer value gives us 123456, which we then divide by 100 to get 1234.56.

Overall, these lines of code ensure that the output values of C, I, and im are rounded to two decimal places for better formatting and readability.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Latest member

Latest Threads