Compute a Single Loan Amortization Step in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute a single loan amortization step in C. The lab covers the fundamental steps required for loan amortization calculation, including reading the principal amount, interest rate, and payment amount, as well as computing the interest portion and principal portion of the payment. By the end of this lab, you will have a better understanding of financial math and interest calculations using C programming.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435340{{"`Compute a Single Loan Amortization Step in C`"}} c/variables -.-> lab-435340{{"`Compute a Single Loan Amortization Step in C`"}} c/user_input -.-> lab-435340{{"`Compute a Single Loan Amortization Step in C`"}} c/math_functions -.-> lab-435340{{"`Compute a Single Loan Amortization Step in C`"}} end

Read Principal, Rate, Payment

In this step, you will learn how to input and read the fundamental parameters required for loan amortization calculation in C: principal amount, interest rate, and payment amount.

First, let's create a C source file for our loan amortization program:

cd ~/project
nano loan_amortization.c

Now, add the following code to the file:

#include <stdio.h>

int main() {
    double principal, rate, payment;

    // Read principal amount
    printf("Enter the loan principal amount: ");
    scanf("%lf", &principal);

    // Read annual interest rate
    printf("Enter the annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    // Read monthly payment amount
    printf("Enter the monthly payment amount: ");
    scanf("%lf", &payment);

    // Display the input values
    printf("\nLoan Details:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate);
    printf("Monthly Payment: $%.2f\n", payment);

    return 0;
}

Compile and run the program:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

Example output:

Enter the loan principal amount: 10000
Enter the annual interest rate (in percentage): 5.5
Enter the monthly payment amount: 200

Loan Details:
Principal: $10000.00
Annual Interest Rate: 5.50%
Monthly Payment: $200.00

Compute Interest Portion and Principal Portion

In this step, you will learn how to calculate the interest and principal portions of a loan payment using financial mathematics formulas.

Open the previous loan_amortization.c file and modify it to include calculation logic:

cd ~/project
nano loan_amortization.c

Update the code with calculation functions:

#include <stdio.h>

// Function to calculate monthly interest
double calculateMonthlyInterest(double principal, double annualRate) {
    double monthlyRate = annualRate / 12 / 100;
    return principal * monthlyRate;
}

// Function to calculate principal portion
double calculatePrincipalPortion(double payment, double monthlyInterest) {
    return payment - monthlyInterest;
}

int main() {
    double principal, rate, payment;
    double monthlyInterest, principalPortion;

    // Previous input code remains the same
    printf("Enter the loan principal amount: ");
    scanf("%lf", &principal);

    printf("Enter the annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    printf("Enter the monthly payment amount: ");
    scanf("%lf", &payment);

    // Calculate interest and principal portions
    monthlyInterest = calculateMonthlyInterest(principal, rate);
    principalPortion = calculatePrincipalPortion(payment, monthlyInterest);

    // Display calculation results
    printf("\nLoan Payment Breakdown:\n");
    printf("Monthly Interest: $%.2f\n", monthlyInterest);
    printf("Principal Portion: $%.2f\n", principalPortion);
    printf("Remaining Principal: $%.2f\n", principal - principalPortion);

    return 0;
}

Compile and run the updated program:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

Example output:

Enter the loan principal amount: 10000
Enter the annual interest rate (in percentage): 5.5
Enter the monthly payment amount: 200

Loan Payment Breakdown:
Monthly Interest: $45.83
Principal Portion: $154.17
Remaining Principal: $9845.83

Print Updated Principal

In this step, you will learn how to track and print the updated principal balance after each loan payment, demonstrating the loan amortization process.

Open the previous loan_amortization.c file and modify it to include multiple payment iterations:

cd ~/project
nano loan_amortization.c

Update the code to simulate multiple loan payments:

#include <stdio.h>

// Previous functions remain the same
double calculateMonthlyInterest(double principal, double annualRate) {
    double monthlyRate = annualRate / 12 / 100;
    return principal * monthlyRate;
}

double calculatePrincipalPortion(double payment, double monthlyInterest) {
    return payment - monthlyInterest;
}

int main() {
    double principal, rate, payment;
    int totalPayments;

    // Input loan details
    printf("Enter the loan principal amount: ");
    scanf("%lf", &principal);

    printf("Enter the annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    printf("Enter the monthly payment amount: ");
    scanf("%lf", &payment);

    printf("Enter the total number of payments: ");
    scanf("%d", &totalPayments);

    // Print initial loan details
    printf("\nInitial Loan Details:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate);
    printf("Monthly Payment: $%.2f\n\n", payment);

    // Simulate loan amortization
    for (int month = 1; month <= totalPayments; month++) {
        double monthlyInterest = calculateMonthlyInterest(principal, rate);
        double principalPortion = calculatePrincipalPortion(payment, monthlyInterest);

        // Update principal
        principal -= principalPortion;

        // Print monthly breakdown
        printf("Payment %d:\n", month);
        printf("Monthly Interest: $%.2f\n", monthlyInterest);
        printf("Principal Portion: $%.2f\n", principalPortion);
        printf("Remaining Principal: $%.2f\n\n", principal);
    }

    return 0;
}

Compile and run the updated program:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

Example output:

Enter the loan principal amount: 10000
Enter the annual interest rate (in percentage): 5.5
Enter the monthly payment amount: 200
Enter the total number of payments: 3

Initial Loan Details:
Principal: $10000.00
Annual Interest Rate: 5.50%
Monthly Payment: $200.00

Payment 1:
Monthly Interest: $45.83
Principal Portion: $154.17
Remaining Principal: $9845.83

Payment 2:
Monthly Interest: $45.04
Principal Portion: $154.96
Remaining Principal: $9690.87

Payment 3:
Monthly Interest: $44.25
Principal Portion: $155.75
Remaining Principal: $9535.12

Summary

In this lab, you will learn how to read the fundamental parameters required for loan amortization calculation in C, including the principal amount, interest rate, and payment amount. You will also learn how to calculate the interest and principal portions of a loan payment using financial mathematics formulas. By the end of this lab, you will have a basic understanding of the steps involved in computing a single loan amortization step in C.

Other C Tutorials you may like