Compute Bond Price Given Yield in C

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to compute the price of a bond given its yield in C. We will start by reading the key bond parameters, including coupon rate, face value, yield, and maturity. Then, we will implement the present value calculation to discount all future bond payments to their current value, which will give us the bond price. Finally, we will print the calculated bond price.

The lab covers fundamental financial math and interest calculations using C programming, providing a practical example of bond pricing that can be applied in various financial applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435341{{"`Compute Bond Price Given Yield in C`"}} c/variables -.-> lab-435341{{"`Compute Bond Price Given Yield in C`"}} c/for_loop -.-> lab-435341{{"`Compute Bond Price Given Yield in C`"}} c/user_input -.-> lab-435341{{"`Compute Bond Price Given Yield in C`"}} c/math_functions -.-> lab-435341{{"`Compute Bond Price Given Yield in C`"}} end

Read Coupon, Face Value, Yield, Maturity

In this step, we will learn how to read and initialize the key parameters required for bond price calculation in C. These parameters include coupon rate, face value, yield, and maturity.

First, let's create a new C file to implement our bond pricing calculation:

cd ~/project
nano bond_price.c

Now, let's write the code to declare and initialize the bond parameters:

#include <stdio.h>

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

    // Print the bond parameters
    printf("Bond Parameters:\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);

    return 0;
}

Compile and run the program:

gcc bond_price.c -o bond_price
./bond_price

Example output:

Bond Parameters:
Coupon Rate: 5.00%
Face Value: $1000.00
Yield to Maturity: 6.00%
Years to Maturity: 5

Discount All Payments to Present

In this step, we will implement the present value calculation for bond payments, which involves discounting coupon payments and face value to their present value.

Update the bond_price.c file to include the present value calculation:

cd ~/project
nano bond_price.c

Modify the code to calculate present value of cash flows:

#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;
}

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 results
    printf("Bond Price Calculation:\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("Bond Price: $%.2f\n", bond_price);

    return 0;
}

Compile the program with math library:

gcc bond_price.c -o bond_price -lm
./bond_price

Example output:

Bond Price Calculation:
Coupon Rate: 5.00%
Face Value: $1000.00
Yield to Maturity: 6.00%
Years to Maturity: 5
Bond Price: $952.08

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%)
===============================

Summary

In this lab, we learned how to read and initialize the key parameters required for bond price calculation in C, including coupon rate, face value, yield, and maturity. We then implemented the present value calculation for bond payments, discounting coupon payments and face value to their present value. Finally, we printed the calculated bond price.

The lab provided a step-by-step guide to computing bond price given yield in C, covering the essential aspects of the bond pricing process. By following the provided instructions, developers can gain a solid understanding of the underlying concepts and apply them to real-world bond pricing scenarios.

Other C Tutorials you may like