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