Bond calculation

Hi, I am trying to write a very simple bond calculation and I dont seem to get it right. I include what I have so far, also I know I didnt add the principal payment payoff but I will do it later on once I am done with coupon cashflow. Basically, I want a programm to calculate yearly cashflow (coupon / (1+ytm)^time) and add them in the end to get the value of the bond. I would appreciate any help pls. Thanks


#include <iostream>
#include <cmath>

using namespace std;

int main () {

int timetm,facevalue,coupon;
float yildtm;
double sum =0;

cout << "Enter time to maturity"<< endl;
cin >> timetm;
cout << "Enter facevalue of the bond" << endl;
cin >> facevalue;
cout << "Enter coupon rate (no decimals): " << endl;
cin >> coupon;
coupon = coupon /100;
cout << "Enter the yiled to maturity" << endl;
cin >> yildtm;
yildtm = yildtm / 100;

float cashflow = coupon / (pow (1+yildtm,timetm));
for (int i=0; i <timetm; i++){
sum = cashflow+cashflow;}
cout << "Your bond is equal to " << sum << endl;

system ("pause");
return 0;


}
Integer division issue?
cashflow is a float so I am guessing you are looking for a decimal number.

But lets break this down

float cashflow = coupon / (pow (1+yildtm,timetm));

float = int / (pow (1+float,int));

So you are going to end up with a truncated result.



Last edited on
my problem is when I run this, it gives me the value of the bond equal to 0, all the time. But I just want it to calculate cash flow each year and add them up to get the present value of the bond. the only thing that increments in the calculation is the timetm, so coupon / (pow (1+yildtm,timetm), will always be the same for each year and the only thing that would increment is timetm each year.thank you guys again.
Yes it would give you a value of 0 most of the time the way it is written. When I compiled I noticed the same results with different inputs.

But what is happening is you are getting small results that are non zero

example 0.0458 and with the formula you have now it would truncate and output 0

You only need to change a few of your vars to doubles to get it to work like you expect.
thank you very much.
Topic archived. No new replies allowed.