Diskontierung jedes Cashflows und Summierung
In diesem Schritt lernen Sie, wie Sie jeden Cashflow diskontieren und den Net Present Value (NPV) berechnen, indem Sie die diskontierten Cashflows in C summieren.
Ändern wir die vorherige Datei npv_calculator.c
, um die NPV-Berechnung hinzuzufügen:
cd ~/project
nano npv_calculator.c
Ersetzen Sie den vorherigen Code durch die folgende Implementierung:
#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++) {
// Diskontierung jedes Cashflows: 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;
// Eingabe der Anzahl der Cashflows
printf("Geben Sie die Anzahl der Cashflows ein (max %d): ", MAX_CASH_FLOWS);
scanf("%d", &num_cash_flows);
// Anzahl der Cashflows validieren
if (num_cash_flows <= 0 || num_cash_flows > MAX_CASH_FLOWS) {
printf("Ungültige Anzahl der Cashflows.\n");
return 1;
}
// Eingabe der Cashflows
for (int i = 0; i < num_cash_flows; i++) {
printf("Geben Sie den Cashflow %d ein: ", i);
scanf("%lf", &cash_flows[i]);
}
// Eingabe des Diskontsatzes
printf("Geben Sie den Diskontsatz (als Dezimalzahl) ein: ");
scanf("%lf", &discount_rate);
// NPV berechnen
double npv = calculate_npv(cash_flows, num_cash_flows, discount_rate);
// Ergebnisse ausgeben
printf("\nZusammenfassung der Eingabe:\n");
printf("Anzahl der Cashflows: %d\n", num_cash_flows);
printf("Diskontsatz: %.2f%%\n", discount_rate * 100);
printf("\nCashflows:\n");
for (int i = 0; i < num_cash_flows; i++) {
double discounted_cf = cash_flows[i] / pow(1 + discount_rate, i);
printf("Cashflow %d: $%.2f (Diskontiert: $%.2f)\n",
i, cash_flows[i], discounted_cf);
}
printf("\nNet Present Value (NPV): $%.2f\n", npv);
return 0;
}
Kompilieren Sie das Programm mit der Mathematikbibliothek:
gcc -o npv_calculator npv_calculator.c -lm
Führen Sie das Programm aus, um die NPV-Berechnung zu testen:
./npv_calculator
Beispielausgabe:
Geben Sie die Anzahl der Cashflows ein (max 10): 3
Geben Sie den Cashflow 0 ein: -1000
Geben Sie den Cashflow 1 ein: 500
Geben Sie den Cashflow 2 ein: 600
Geben Sie den Diskontsatz (als Dezimalzahl) ein: 0.1
Zusammenfassung der Eingabe:
Anzahl der Cashflows: 3
Diskontsatz: 10.00%
Cashflows:
Cashflow 0: $-1000.00 (Diskontiert: $-1000.00)
Cashflow 1: $500.00 (Diskontiert: $454.55)
Cashflow 2: $600.00 (Diskontiert: $495.87)
Net Present Value (NPV): $-49.58
Wichtige Punkte in dieser Implementierung:
- Hinzufügen der Funktion
calculate_npv()
zur Berechnung des NPV
- Verwendung der Funktion
pow()
zur Diskontierung der Cashflows
- Anzeige sowohl der ursprünglichen als auch der diskontierten Cashflows
- Berechnung und Anzeige des endgültigen NPV
Hinweis: Der Flag -lm
wird verwendet, um die Mathematikbibliothek für die Funktion pow()
zu verknüpfen.