Introduction
In this lab, you will learn how to compute the Net Present Value (NPV) in C. The lab will guide you through the process of reading a series of cash flows and the discount rate, discounting each cash flow, and then summing them to calculate the NPV. You will create a C program that allows users to input the necessary data and then displays the calculated NPV. This lab covers the fundamental concepts of financial mathematics and interest calculations using C programming.
Read Series of Cash Flows and Rate
In this step, you'll learn how to read a series of cash flows and the interest rate for calculating Net Present Value (NPV) in C. We'll create a program that allows users to input multiple cash flows and the discount rate.
First, let's create a new C file for our NPV calculation:
cd ~/project
nano npv_calculator.c
Now, add the following code to the file:
#include <stdio.h>
#define MAX_CASH_FLOWS 10
int main() {
double cash_flows[MAX_CASH_FLOWS];
double discount_rate;
int num_cash_flows;
// Input the number of cash flows
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
// Validate number of cash flows
if (num_cash_flows <= 0 || num_cash_flows > MAX_CASH_FLOWS) {
printf("Invalid number of cash flows.\n");
return 1;
}
// Input cash flows
for (int i = 0; i < num_cash_flows; i++) {
printf("Enter cash flow %d: ", i);
scanf("%lf", &cash_flows[i]);
}
// Input discount rate
printf("Enter the discount rate (as a decimal): ");
scanf("%lf", &discount_rate);
// Print the input for verification
printf("\nInput Summary:\n");
printf("Number of Cash Flows: %d\n", num_cash_flows);
printf("Discount Rate: %.2f%%\n", discount_rate * 100);
printf("\nCash Flows:\n");
for (int i = 0; i < num_cash_flows; i++) {
printf("Cash Flow %d: $%.2f\n", i, cash_flows[i]);
}
return 0;
}
Compile the program:
gcc -o npv_calculator npv_calculator.c
Run the program to test input:
./npv_calculator
Example output:
Enter the number of cash flows (max 10): 3
Enter cash flow 0: -1000
Enter cash flow 1: 500
Enter cash flow 2: 600
Enter the discount rate (as a decimal): 0.1
Input Summary:
Number of Cash Flows: 3
Discount Rate: 10.00%
Cash Flows:
Cash Flow 0: $-1000.00
Cash Flow 1: $500.00
Cash Flow 2: $600.00
This code demonstrates how to:
- Define a maximum number of cash flows
- Read the number of cash flows from user input
- Input individual cash flow values
- Input the discount rate
- Validate and display the input for verification
The program sets up the foundation for NPV calculation by capturing the essential input parameters.
Discount Each Cash Flow and Sum
In this step, you'll learn how to discount each cash flow and calculate the Net Present Value (NPV) by summing the discounted cash flows in C.
Let's modify the previous npv_calculator.c file to add NPV calculation:
cd ~/project
nano npv_calculator.c
Replace the previous code with the following implementation:
#include <stdio.h>
#include <math.h>
#define MAX_CASH_FLOWS 10
double calculate_npv(double cash_flows[], int num_cash_flows, double discount_rate) {
double npv = 0.0;
for (int i = 0; i < num_cash_flows; i++) {
// Discount each cash flow: CF / (1 + r)^t
double discounted_cash_flow = cash_flows[i] / pow(1 + discount_rate, i);
npv += discounted_cash_flow;
}
return npv;
}
int main() {
double cash_flows[MAX_CASH_FLOWS];
double discount_rate;
int num_cash_flows;
// Input the number of cash flows
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
// Validate number of cash flows
if (num_cash_flows <= 0 || num_cash_flows > MAX_CASH_FLOWS) {
printf("Invalid number of cash flows.\n");
return 1;
}
// Input cash flows
for (int i = 0; i < num_cash_flows; i++) {
printf("Enter cash flow %d: ", i);
scanf("%lf", &cash_flows[i]);
}
// Input discount rate
printf("Enter the discount rate (as a decimal): ");
scanf("%lf", &discount_rate);
// Calculate NPV
double npv = calculate_npv(cash_flows, num_cash_flows, discount_rate);
// Print results
printf("\nInput Summary:\n");
printf("Number of Cash Flows: %d\n", num_cash_flows);
printf("Discount Rate: %.2f%%\n", discount_rate * 100);
printf("\nCash Flows:\n");
for (int i = 0; i < num_cash_flows; i++) {
double discounted_cf = cash_flows[i] / pow(1 + discount_rate, i);
printf("Cash Flow %d: $%.2f (Discounted: $%.2f)\n",
i, cash_flows[i], discounted_cf);
}
printf("\nNet Present Value (NPV): $%.2f\n", npv);
return 0;
}
Compile the program with math library:
gcc -o npv_calculator npv_calculator.c -lm
Run the program to test NPV calculation:
./npv_calculator
Example output:
Enter the number of cash flows (max 10): 3
Enter cash flow 0: -1000
Enter cash flow 1: 500
Enter cash flow 2: 600
Enter the discount rate (as a decimal): 0.1
Input Summary:
Number of Cash Flows: 3
Discount Rate: 10.00%
Cash Flows:
Cash Flow 0: $-1000.00 (Discounted: $-1000.00)
Cash Flow 1: $500.00 (Discounted: $454.55)
Cash Flow 2: $600.00 (Discounted: $495.87)
Net Present Value (NPV): $-49.58
Key points in this implementation:
- Added
calculate_npv()function to compute NPV - Used
pow()function to discount cash flows - Displayed both original and discounted cash flows
- Calculated and displayed the final NPV
Note: The -lm flag is used to link the math library for pow() function.
Print NPV
In this final step, you'll learn how to interpret and present the Net Present Value (NPV) calculation results in a user-friendly format.
Let's modify the npv_calculator.c to enhance the output presentation:
cd ~/project
nano npv_calculator.c
Update the code with improved output formatting:
#include <stdio.h>
#include <math.h>
#define MAX_CASH_FLOWS 10
double calculate_npv(double cash_flows[], int num_cash_flows, double discount_rate) {
double npv = 0.0;
for (int i = 0; i < num_cash_flows; i++) {
double discounted_cash_flow = cash_flows[i] / pow(1 + discount_rate, i);
npv += discounted_cash_flow;
}
return npv;
}
void print_npv_analysis(double cash_flows[], int num_cash_flows, double discount_rate, double npv) {
printf("\n--- NPV Analysis Report ---\n");
printf("Discount Rate: %.2f%%\n", discount_rate * 100);
printf("\nCash Flow Breakdown:\n");
printf("--------------------\n");
for (int i = 0; i < num_cash_flows; i++) {
double discounted_cf = cash_flows[i] / pow(1 + discount_rate, i);
printf("Period %d: $%.2f (Discounted: $%.2f)\n",
i, cash_flows[i], discounted_cf);
}
printf("\nNet Present Value (NPV): $%.2f\n", npv);
// Interpret NPV result
if (npv > 0) {
printf("Investment Recommendation: ACCEPT\n");
printf("The project is expected to create value.\n");
} else if (npv < 0) {
printf("Investment Recommendation: REJECT\n");
printf("The project is expected to destroy value.\n");
} else {
printf("Investment Recommendation: NEUTRAL\n");
printf("The project breaks even at the given discount rate.\n");
}
}
int main() {
double cash_flows[MAX_CASH_FLOWS];
double discount_rate;
int num_cash_flows;
printf("Net Present Value (NPV) Calculator\n");
printf("==================================\n");
// Input the number of cash flows
printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
// Validate number of cash flows
if (num_cash_flows <= 0 || num_cash_flows > MAX_CASH_FLOWS) {
printf("Invalid number of cash flows.\n");
return 1;
}
// Input cash flows
for (int i = 0; i < num_cash_flows; i++) {
printf("Enter cash flow for period %d: ", i);
scanf("%lf", &cash_flows[i]);
}
// Input discount rate
printf("Enter the discount rate (as a decimal): ");
scanf("%lf", &discount_rate);
// Calculate NPV
double npv = calculate_npv(cash_flows, num_cash_flows, discount_rate);
// Print NPV analysis
print_npv_analysis(cash_flows, num_cash_flows, discount_rate, npv);
return 0;
}
Compile the program:
gcc -o npv_calculator npv_calculator.c -lm
Run the program to test the enhanced NPV output:
./npv_calculator
Example output:
Net Present Value (NPV) Calculator
==================================
Enter the number of cash flows (max 10): 3
Enter cash flow for period 0: -1000
Enter cash flow for period 1: 500
Enter cash flow for period 2: 600
Enter the discount rate (as a decimal): 0.1
--- NPV Analysis Report ---
Discount Rate: 10.00%
Cash Flow Breakdown:
--------------------
Period 0: $-1000.00 (Discounted: $-1000.00)
Period 1: $500.00 (Discounted: $454.55)
Period 2: $600.00 (Discounted: $495.87)
Net Present Value (NPV): $-49.58
Investment Recommendation: REJECT
The project is expected to destroy value.
Key improvements in this version:
- Added a dedicated
print_npv_analysis()function - Enhanced output formatting
- Added investment recommendation based on NPV
- Improved user interface with clear headings
Summary
In this lab, you learned how to read a series of cash flows and the interest rate for calculating Net Present Value (NPV) in C. You created a program that allows users to input multiple cash flows and the discount rate, and then the program prints the input for verification.
Next, you will learn how to discount each cash flow and sum them to calculate the NPV, and then print the final result.



