Write an algorithm that calculates the monthly payment of a bank loan with a fixed interest-rate. Given the principal amount, the fixed interest rate, the number of years to pay the loan, you can calculate the monthly payment as: ππππ‘βππ¦ πππ¦ππππ‘=(ππππππππππ΄πππ’ππ‘ΓπΌππ‘ππππ π‘π ππ‘πΓ#π¦ππππ +ππππππππππ΄πππ’ππ‘)/#ππππ‘βπ Your program should print the required monthly payment of this loan
$\begingroup$
$\endgroup$
2
-
4$\begingroup$ I'm voting to close this post as off-topic because it is a homework-style exercise, not a question. $\endgroup$– David RicherbyCommented Oct 7, 2019 at 23:13
-
$\begingroup$ This is a question-and-answer site, so we require you to articulate a specific question about your exercise -- we're not looking for posts that are just a paste of an exercise-style question. $\endgroup$– D.W. ♦Commented Oct 8, 2019 at 1:17
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
3
Here is working code. Don't forget to input your percentage in fractional form (e.g. $0.02$ for $2\%$).
#include <stdio.h>
#define MONTHS_IN_YEAR 12
float monthlyRepaymentAmount(float principalAmount, float interestRate, int numberOfYears);
int main(void) {
//Problem statement variables
float principalAmount; // use int if principal can only be an integer
float interestRate;
int numberOfYears;
//Ask user for problem statement variables
printf("Please enter the principal amount of the loan: \n");
scanf("%f", &principalAmount);
if (principalAmount <= 0) return -1;
printf("Please enter the interest rate: \n");
scanf("%f", &interestRate);
if (interestRate <= 0) return -1;
printf("Please enter the number of years (term of the loan): \n");
scanf("%d", &numberOfYears);
if (numberOfYears <= 0) return -1;
//Compute and output result
printf("You need to make monthly loan repayments of $%f \n", \
monthlyRepaymentAmount(principalAmount, interestRate, numberOfYears));
return 0;
}
float monthlyRepaymentAmount(float principalAmount, float interestRate, int numberOfYears) {
return (principalAmount * interestRate * numberOfYears \
+ principalAmount) / (numberOfYears * MONTHS_IN_YEAR);
}
-
4$\begingroup$ Please don't answer off-topic questions: we're not a homework cheating site, and a pile of code is basically never an answer here. $\endgroup$ Commented Oct 7, 2019 at 23:12
-
$\begingroup$ Just a hint: you can delete your answer and in the future reconsider help with homework. In the long run it is harmful to OP. Here code without explanation means either that question is off-topic or textual content is missing in the answer. $\endgroup$– EvilCommented Oct 8, 2019 at 3:14
-
1$\begingroup$ My bad, although I tried to make the code as clear and commented as possible through comments, it's true that this is a homework style question. I cannot delete it anymore unfortunately as it has been accepted, but if someone who can sees this please do! $\endgroup$ Commented Oct 8, 2019 at 11:12