229k views
0 votes
Get principal,rate and term from the user,including input validations. Then pass these values as arguments to a function in which you calculate the monthly payment of the loan and return the monthly payment from this function to display the loan information with the monthly payment.Using R programming.

User Fluffy
by
8.7k points

1 Answer

5 votes

Final answer:

To calculate the monthly payment of a loan in R, you must gather the loan's principal, interest rate, and term, and use them within a function that applies the monthly payment formula.

Step-by-step explanation:

To calculate the monthly payment of a loan using R programming language, you need to collect the principal amount, the interest rate (annual), and the term of the loan in years. Then, you use these values as arguments for a function that calculates the monthly payment.

The formula to calculate the monthly payment (R) on a loan is:
R = P * (i * (1 + i)^n) / ((1 + i)^n - 1)

Where

For instance, if you have a principal (P) of $300,000 with an annual interest rate (rate) of 6% over a term of 30 years, the monthly interest rate (i) is 0.06/12 and the number of payments (n) is 30*12.

To find the monthly payment:

monthly_payment <- function(principal, annual_rate, term_years) {
i <- annual_rate / 12
n <- term_years * 12
P * (i * (1 + i)^n) / ((1 + i)^n - 1)
}
R <- monthly_payment(300000, 0.06, 30)

The monthly_payment function calculates the monthly payment, which can then be displayed along with the loan information.

User Peter Kovac
by
7.9k points