Introduction
In this lab, you will learn how to compute mortgage payments using C programming. The lab covers the essential steps, including reading the principal amount, interest rate, and number of payments, and then applying the mortgage payment calculation formula to determine the monthly payment. By the end of this lab, you will have a working C program that can calculate mortgage payments based on user input.
The lab walks through the complete process, from creating a new C file to implementing the payment calculation and printing the result. This hands-on exercise will help you develop your skills in financial math and interest calculations using C programming.
Read Principal, Rate, Number of Payments
In this step, we'll learn how to read the essential inputs for calculating mortgage payments: principal amount, interest rate, and the number of payments.
First, let's create a new C file for our mortgage payment calculator:
cd ~/project
nano mortgage_calculator.c
Now, let's write the code to read the input values:
#include <stdio.h>
int main() {
double principal, rate;
int num_payments;
// Prompt and read principal amount
printf("Enter the loan principal amount: ");
scanf("%lf", &principal);
// Prompt and read annual interest rate
printf("Enter the annual interest rate (as a decimal): ");
scanf("%lf", &rate);
// Prompt and read number of payments
printf("Enter the total number of payments: ");
scanf("%d", &num_payments);
// Print the input values to verify
printf("\nLoan Details:\n");
printf("Principal: $%.2f\n", principal);
printf("Annual Interest Rate: %.2f%%\n", rate * 100);
printf("Number of Payments: %d\n", num_payments);
return 0;
}
Compile and run the program:
gcc mortgage_calculator.c -o mortgage_calculator
./mortgage_calculator
Example output:
Enter the loan principal amount: 200000
Enter the annual interest rate (as a decimal): 0.05
Enter the total number of payments: 360
Loan Details:
Principal: $200000.00
Annual Interest Rate: 5.00%
Number of Payments: 360
Let's break down the code:
- We use
doublefor principal and rate to handle decimal values scanf()is used to read user inputs%lfformat specifier is used for double values%dis used for integer number of payments- We print the inputs to verify they were correctly captured
Payment = P*(R*(1+R)^N)/((1+R)^N -1)
In this step, we'll implement the mortgage payment calculation formula using the inputs from the previous step.
Let's modify the existing mortgage_calculator.c file:
cd ~/project
nano mortgage_calculator.c
Update the code to calculate the monthly payment:
#include <stdio.h>
#include <math.h>
double calculate_mortgage_payment(double principal, double rate, int num_payments) {
// Monthly interest rate
double monthly_rate = rate / 12;
// Calculate payment using the mortgage formula
double payment = principal *
(monthly_rate * pow(1 + monthly_rate, num_payments)) /
(pow(1 + monthly_rate, num_payments) - 1);
return payment;
}
int main() {
double principal, rate;
int num_payments;
// Prompt and read principal amount
printf("Enter the loan principal amount: ");
scanf("%lf", &principal);
// Prompt and read annual interest rate
printf("Enter the annual interest rate (as a decimal): ");
scanf("%lf", &rate);
// Prompt and read number of payments
printf("Enter the total number of payments: ");
scanf("%d", &num_payments);
// Calculate monthly payment
double monthly_payment = calculate_mortgage_payment(principal, rate, num_payments);
// Print the input values and calculated payment
printf("\nLoan Details:\n");
printf("Principal: $%.2f\n", principal);
printf("Annual Interest Rate: %.2f%%\n", rate * 100);
printf("Number of Payments: %d\n", num_payments);
printf("Monthly Payment: $%.2f\n", monthly_payment);
return 0;
}
Compile the program with math library:
gcc mortgage_calculator.c -o mortgage_calculator -lm
Run the program:
./mortgage_calculator
Example output:
Enter the loan principal amount: 200000
Enter the annual interest rate (as a decimal): 0.05
Enter the total number of payments: 360
Loan Details:
Principal: $200000.00
Annual Interest Rate: 5.00%
Number of Payments: 360
Monthly Payment: $1073.64
Key points about the mortgage payment formula:
Pis the principal loan amountRis the monthly interest rate (annual rate divided by 12)Nis the total number of paymentspow()function from<math.h>is used to calculate exponents- We compile with
-lmto link the math library
Print the Payment
In this final step, we'll enhance our mortgage calculator to provide a detailed payment breakdown and format the output for better readability.
Let's modify the mortgage_calculator.c file to add more comprehensive payment information:
cd ~/project
nano mortgage_calculator.c
Update the code with improved payment printing:
#include <stdio.h>
#include <math.h>
double calculate_mortgage_payment(double principal, double rate, int num_payments) {
// Monthly interest rate
double monthly_rate = rate / 12;
// Calculate payment using the mortgage formula
double payment = principal *
(monthly_rate * pow(1 + monthly_rate, num_payments)) /
(pow(1 + monthly_rate, num_payments) - 1);
return payment;
}
void print_payment_details(double principal, double rate, int num_payments, double monthly_payment) {
double total_payment = monthly_payment * num_payments;
double total_interest = total_payment - principal;
printf("\n--- Mortgage Payment Breakdown ---\n");
printf("Loan Principal: $%10.2f\n", principal);
printf("Annual Interest Rate: %10.2f%%\n", rate * 100);
printf("Total Number of Payments: %d\n", num_payments);
printf("Monthly Payment: $%10.2f\n", monthly_payment);
printf("Total Payments: $%10.2f\n", total_payment);
printf("Total Interest Paid: $%10.2f\n", total_interest);
}
int main() {
double principal, rate;
int num_payments;
// Prompt and read principal amount
printf("Enter the loan principal amount: ");
scanf("%lf", &principal);
// Prompt and read annual interest rate
printf("Enter the annual interest rate (as a decimal): ");
scanf("%lf", &rate);
// Prompt and read number of payments
printf("Enter the total number of payments: ");
scanf("%d", &num_payments);
// Calculate monthly payment
double monthly_payment = calculate_mortgage_payment(principal, rate, num_payments);
// Print detailed payment information
print_payment_details(principal, rate, num_payments, monthly_payment);
return 0;
}
Compile the program:
gcc mortgage_calculator.c -o mortgage_calculator -lm
Run the program:
./mortgage_calculator
Example output:
Enter the loan principal amount: 200000
Enter the annual interest rate (as a decimal): 0.05
Enter the total number of payments: 360
--- Mortgage Payment Breakdown ---
Loan Principal: $ 200000.00
Annual Interest Rate: 5.00%
Total Number of Payments: 360
Monthly Payment: $ 1073.64
Total Payments: $ 386510.40
Total Interest Paid: $ 186510.40
Key improvements:
- Added a new function
print_payment_details()for formatted output - Calculate total payments and total interest
- Use formatting to align decimal values
- Provide a clear, readable breakdown of mortgage details
Summary
In this lab, we learned how to read the essential inputs for calculating mortgage payments, including the principal amount, interest rate, and the number of payments. We then implemented the mortgage payment calculation formula using these inputs to compute the monthly payment amount. Finally, we printed the calculated payment to display the result.
The key steps covered in this lab are: reading the principal, rate, and number of payments from user input, and then applying the mortgage payment formula to compute the monthly payment. The program ensures the inputs are correctly captured and provides a clear output of the loan details and the calculated payment.



