Introduction
In this lab, we will learn how to approximate the Internal Rate of Return (IRR) using a C program. We will start by reading the cash flows, which represent the money invested or received at different time periods of an investment. Then, we will use an iterative approach to find the rate where the Net Present Value (NPV) is approximately zero, which gives us the estimated IRR. Finally, we will print the calculated IRR. The lab covers key financial math concepts and demonstrates their implementation in C programming.
Read Cash Flows
In this step, we'll learn how to read and store cash flows for calculating the Internal Rate of Return (IRR) in a C program. Cash flows represent the money invested or received at different time periods of an investment.
First, let's create a C file to implement our cash flow reading functionality:
cd ~/project
nano irr_calculation.c
Now, let's write the initial code to read cash flows:
#include <stdio.h>
#define MAX_CASH_FLOWS 10
int main() {
double cash_flows[MAX_CASH_FLOWS];
int num_cash_flows;
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
printf("Enter cash flows (negative for investments, positive for returns):\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash flow %d: ", i);
scanf("%lf", &cash_flows[i]);
}
// Print the entered cash flows for verification
printf("\nEntered Cash Flows:\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash flow %d: %.2f\n", i, cash_flows[i]);
}
return 0;
}
Compile and run the program:
gcc irr_calculation.c -o irr_calculation
./irr_calculation
Example output:
Enter the number of cash flows (max 10): 4
Enter cash flows (negative for investments, positive for returns):
Cash flow 0: -1000
Cash flow 1: 300
Cash flow 2: 400
Cash flow 3: 500
Entered Cash Flows:
Cash flow 0: -1000.00
Cash flow 1: 300.00
Cash flow 2: 400.00
Cash flow 3: 500.00
Explanation
- We define a maximum number of cash flows (
MAX_CASH_FLOWS) to prevent excessive memory usage - The program first asks the user to input the number of cash flows
- Then it prompts the user to enter each cash flow value
- Negative values represent initial investments
- Positive values represent returns or income
- The program prints back the entered cash flows for verification
Use Iteration to Find Rate Where NPV≈0
In this step, we'll extend our previous cash flow program to calculate the Internal Rate of Return (IRR) using an iterative numerical method.
First, let's modify our existing C file:
cd ~/project
nano irr_calculation.c
Now, implement the NPV and IRR calculation logic:
#include <stdio.h>
#include <math.h>
#define MAX_CASH_FLOWS 10
#define EPSILON 0.0001
double calculate_npv(double cash_flows[], int num_cash_flows, double rate) {
double npv = 0.0;
for (int i = 0; i < num_cash_flows; i++) {
npv += cash_flows[i] / pow(1 + rate, i);
}
return npv;
}
double find_irr(double cash_flows[], int num_cash_flows) {
double rate_low = -0.9;
double rate_high = 10.0;
double rate = 0.1;
while ((rate_high - rate_low) > EPSILON) {
double npv = calculate_npv(cash_flows, num_cash_flows, rate);
if (fabs(npv) < EPSILON) {
return rate;
}
if (npv > 0) {
rate_low = rate;
} else {
rate_high = rate;
}
rate = (rate_low + rate_high) / 2.0;
}
return rate;
}
int main() {
double cash_flows[MAX_CASH_FLOWS];
int num_cash_flows;
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
printf("Enter cash flows (negative for investments, positive for returns):\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash flow %d: ", i);
scanf("%lf", &cash_flows[i]);
}
double irr = find_irr(cash_flows, num_cash_flows);
printf("\nApproximate Internal Rate of Return (IRR): %.4f or %.2f%%\n", irr, irr * 100);
return 0;
}
Compile the program with math library:
gcc irr_calculation.c -o irr_calculation -lm
Run the program with sample cash flows:
./irr_calculation
Example output:
Enter the number of cash flows (max 10): 4
Enter cash flows (negative for investments, positive for returns):
Cash flow 0: -1000
Cash flow 1: 300
Cash flow 2: 400
Cash flow 3: 500
Approximate Internal Rate of Return (IRR): 0.2154 or 21.54%
Explanation
calculate_npv()computes the Net Present Value for a given interest ratefind_irr()uses bisection method to find the rate where NPV ≈ 0- We use
EPSILONto define convergence precision - The algorithm iteratively narrows down the IRR range
- Final IRR is calculated and displayed as a decimal and percentage
Print Estimated IRR
In this final step, we'll enhance our IRR calculation program to provide more detailed output and demonstrate different investment scenarios.
Let's modify our existing C file to add more comprehensive IRR analysis:
cd ~/project
nano irr_calculation.c
Update the code with additional output and analysis:
#include <stdio.h>
#include <math.h>
#define MAX_CASH_FLOWS 10
#define EPSILON 0.0001
double calculate_npv(double cash_flows[], int num_cash_flows, double rate) {
double npv = 0.0;
for (int i = 0; i < num_cash_flows; i++) {
npv += cash_flows[i] / pow(1 + rate, i);
}
return npv;
}
double find_irr(double cash_flows[], int num_cash_flows) {
double rate_low = -0.9;
double rate_high = 10.0;
double rate = 0.1;
while ((rate_high - rate_low) > EPSILON) {
double npv = calculate_npv(cash_flows, num_cash_flows, rate);
if (fabs(npv) < EPSILON) {
return rate;
}
if (npv > 0) {
rate_low = rate;
} else {
rate_high = rate;
}
rate = (rate_low + rate_high) / 2.0;
}
return rate;
}
void print_investment_analysis(double cash_flows[], int num_cash_flows, double irr) {
double total_investment = 0;
double total_returns = 0;
printf("\n--- Investment Analysis ---\n");
// Detailed cash flow breakdown
for (int i = 0; i < num_cash_flows; i++) {
printf("Period %d: $%.2f\n", i, cash_flows[i]);
if (cash_flows[i] < 0) {
total_investment += fabs(cash_flows[i]);
} else {
total_returns += cash_flows[i];
}
}
// Investment summary
printf("\nTotal Investment: $%.2f\n", total_investment);
printf("Total Returns: $%.2f\n", total_returns);
printf("Net Profit: $%.2f\n", total_returns - total_investment);
// IRR details
printf("\nInternal Rate of Return (IRR):\n");
printf("Decimal: %.4f\n", irr);
printf("Percentage: %.2f%%\n", irr * 100);
// Investment performance interpretation
if (irr > 0.15) {
printf("\nInvestment Performance: Excellent\n");
} else if (irr > 0.10) {
printf("\nInvestment Performance: Good\n");
} else if (irr > 0) {
printf("\nInvestment Performance: Moderate\n");
} else {
printf("\nInvestment Performance: Poor\n");
}
}
int main() {
double cash_flows[MAX_CASH_FLOWS];
int num_cash_flows;
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
printf("Enter cash flows (negative for investments, positive for returns):\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash flow %d: ", i);
scanf("%lf", &cash_flows[i]);
}
double irr = find_irr(cash_flows, num_cash_flows);
print_investment_analysis(cash_flows, num_cash_flows, irr);
return 0;
}
Compile the program:
gcc irr_calculation.c -o irr_calculation -lm
Run the program with sample investment scenario:
./irr_calculation
Example output:
Enter the number of cash flows (max 10): 4
Enter cash flows (negative for investments, positive for returns):
Cash flow 0: -1000
Cash flow 1: 300
Cash flow 2: 400
Cash flow 3: 500
--- Investment Analysis ---
Period 0: $-1000.00
Period 1: $300.00
Period 2: $400.00
Period 3: $500.00
Total Investment: $1000.00
Total Returns: $1200.00
Net Profit: $200.00
Internal Rate of Return (IRR):
Decimal: 0.2154
Percentage: 21.54%
Investment Performance: Excellent
Explanation
- Added
print_investment_analysis()function to provide comprehensive output - Calculates total investment, returns, and net profit
- Interprets IRR performance with descriptive categories
- Provides detailed breakdown of cash flows and investment metrics
Summary
In this lab, we first learned how to read and store cash flows for calculating the Internal Rate of Return (IRR) in a C program. Cash flows represent the money invested or received at different time periods of an investment. We then explored using iteration to find the rate where the Net Present Value (NPV) is approximately zero, which is the IRR. Finally, we learned how to print the estimated IRR. The key learning points are understanding cash flow data structures, implementing iterative algorithms to find the IRR, and presenting the final result.



