Print Bond Price
In this step, we will enhance our bond pricing program to provide a more detailed and formatted output of the bond price calculation.
Update the bond_price.c
file to improve the output formatting:
cd ~/project
nano bond_price.c
Modify the code to add detailed bond price presentation:
#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;
// Calculate coupon payment
double coupon_payment = coupon_rate * face_value;
// Discount coupon payments
for (int year = 1; year <= maturity; year++) {
double present_value_coupon = coupon_payment / pow(1 + yield, year);
bond_price += present_value_coupon;
}
// Discount face value
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("===== Bond Pricing Analysis =====\n");
printf("Coupon Rate: %.2f%%\n", coupon_rate * 100);
printf("Face Value: $%.2f\n", face_value);
printf("Yield to Maturity: %.2f%%\n", yield * 100);
printf("Years to Maturity: %d\n", maturity);
printf("--------------------------------\n");
printf("Calculated Bond Price: $%.2f\n", bond_price);
printf("Discount from Par: $%.2f (%.2f%%)\n",
face_value - bond_price,
((face_value - bond_price) / face_value) * 100);
printf("===============================\n");
}
int main() {
// Bond parameters
double coupon_rate = 0.05; // 5% annual coupon rate
double face_value = 1000.0; // Bond face value
double yield = 0.06; // Annual yield to maturity
int maturity = 5; // Years to maturity
// Calculate bond price
double bond_price = calculate_bond_price(coupon_rate, face_value, yield, maturity);
// Print detailed bond price information
print_bond_details(coupon_rate, face_value, yield, maturity, bond_price);
return 0;
}
Compile and run the program:
gcc bond_price.c -o bond_price -lm
./bond_price
Example output:
===== Bond Pricing Analysis =====
Coupon Rate: 5.00%
Face Value: $1000.00
Yield to Maturity: 6.00%
Years to Maturity: 5
--------------------------------
Calculated Bond Price: $952.08
Discount from Par: $47.92 (4.79%)
===============================