-3
$\begingroup$

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

$\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$ Commented 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

1 Answer 1

-4
$\begingroup$

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);
}

$\endgroup$
3
  • 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$
    – Evil
    Commented 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$
    – David Cian
    Commented Oct 8, 2019 at 11:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.