Anleihepreis ausgeben
In diesem Schritt erweitern wir unser Anleihepreisprogramm, um eine detailliertere und formatierte Ausgabe der Anleihepreisberechnung zu liefern.
Aktualisieren Sie die Datei bond_price.c
, um die Ausgabeformatierung zu verbessern:
cd ~/project
nano bond_price.c
Ändern Sie den Code, um eine detaillierte Darstellung des Anleihepreises hinzuzufügen:
#include <stdio.h>
#include <math.h>
double calculate_bond_price(double coupon_rate, double face_value,
double yield, int maturity) {
double bond_price = 0.0;
// Kuponzahlung berechnen
double coupon_payment = coupon_rate * face_value;
// Kuponzahlungen diskontieren
for (int year = 1; year <= maturity; year++) {
double present_value_coupon = coupon_payment / pow(1 + yield, year);
bond_price += present_value_coupon;
}
// Nennwert diskontieren
double present_value_face = face_value / pow(1 + yield, maturity);
bond_price += present_value_face;
return bond_price;
}
void print_bond_details(double coupon_rate, double face_value,
double yield, int maturity, double bond_price) {
printf("===== Anleihepreisanalyse =====\n");
printf("Kuponrate: %.2f%%\n", coupon_rate * 100);
printf("Nennwert: $%.2f\n", face_value);
printf("Rendite bis zur Fälligkeit: %.2f%%\n", yield * 100);
printf("Jahre bis zur Fälligkeit: %d\n", maturity);
printf("--------------------------------\n");
printf("Berechneter Anleihepreis: $%.2f\n", bond_price);
printf("Abzinsung vom Nennwert: $%.2f (%.2f%%)\n",
face_value - bond_price,
((face_value - bond_price) / face_value) * 100);
printf("===============================\n");
}
int main() {
// Anleiheparameter
double coupon_rate = 0.05; // 5% jährliche Kuponrate
double face_value = 1000.0; // Nennwert der Anleihe
double yield = 0.06; // Jährliche Rendite bis zur Fälligkeit
int maturity = 5; // Jahre bis zur Fälligkeit
// Anleihepreis berechnen
double bond_price = calculate_bond_price(coupon_rate, face_value, yield, maturity);
// Detaillierte Anleihepreisinformationen ausgeben
print_bond_details(coupon_rate, face_value, yield, maturity, bond_price);
return 0;
}
Kompilieren und ausführen des Programms:
gcc bond_price.c -o bond_price -lm
./bond_price
Beispielausgabe:
===== Anleihepreisanalyse =====
Kuponrate: 5.00%
Nennwert: $1000.00
Rendite bis zur Fälligkeit: 6.00%
Jahre bis zur Fälligkeit: 5
--------------------------------
Berechneter Anleihepreis: $952.08
Abzinsung vom Nennwert: $47.92 (4.79%)
===============================