Pricing a Bond

Hello, I have written a code that prices a bond, but its pretty messy, is there anyway to condense the code somehow, if yes how? Thank you in advance.

"Write a function that will return a present value of a following bond. The bond pays 12 coupons per year. The first coupon in a given year pays off $1, the second coupon pays off $2, the third coupon pays off $3, etc. The bond face value is $200. The function should take as arguments: yield to maturity (ytm) and time to maturity in years (n). Implement this function the the main() function with ytm = 0.12 and n = 15. Report both the code and the price of the bond in the email submission."

#include <iostream>
#include <cmath>
#include<ctime>
#include<cstdlib>

int draw();
double discounte_random_coupons(unsigned int n, double ytm);

int main(){

srand(time(NULL));
unsigned int n;
double ytm;
double approximate_price_of_bond;

double rollingSum = 0.0;

const int nReps = 100000;


std::cout << "Enter time to maturity in years\n";
std::cin >> n;
std::cout << "Enter yield to maturity\n";
std::cin >> ytm;

for(int i = 0; i < nReps; i++){
double result = discounte_random_coupons(n, ytm);
rollingSum += result;
}
approximate_price_of_bond=rollingSum/nReps +200/pow((1+ytm/12), double (12*n));



std::cout << "\n\n Approximate price of a bond is equal to " << approximate_price_of_bond << "\n";

std::cout << "Press Enter to continue...\n";
std::cin.ignore(100,'\n');
std::cin.get();

return 0;

}


double discounte_random_coupons(unsigned int n, double ytm){

double price1 = 0, result1;
double price2 = 0, result2;
double price3 = 0, result3;
double price4 = 0, result4;
double price5 = 0, result5;
double price6 = 0, result6;
double price7 = 0, result7;
double price8 = 0, result8;
double price9 = 0, result9;
double price10 = 0, result10;
double price11 = 0, result11;
double price12 = 0, result12;
double total_price_without_face_value=0;
//discounting all the coupons
double i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12;
i1=1; i2=2; i3=3; i4=4; i5=5; i6=6; i7=7; i8=8; i9=9; i10=10; i11=11; i12=12;

while ( i1 <= (n*12-12+1))
{
result1 = draw()/pow((1+ytm/12),i1);
i1=i1+12;
price1=price1+result1;
}


while ( i2 <= (n*12-12+2))
{
result2= draw()/pow((1+ytm/12),i2);
i2=i2+12;
price2=price2+result2;
}

while ( i3 <= (n*12-12+3)){
result3= draw()/pow((1+ytm/12),i3);
i3=i3+12;
price3=price3+result3;
}

while ( i4 <= (n*12-12+4)){
result4= draw()/pow((1+ytm/12),i4);
i4=i4+12;
price4=price4+result4;
}

while ( i5 <= (n*12-12+5)){
result5= draw()/pow((1+ytm/12),i5);
i5=i5+12;
price5=price5+result5;
}

while ( i6 <= (n*12-12+6)){
result6= draw()/pow((1+ytm/12),i6);
i6=i6+12;
price6=price6+result6;
}

while ( i7 <= (n*12-12+7)){
result7= draw()/pow((1+ytm/12), i7);
i7=i7+12;
price7=price7+result7;
}

while ( i8 <= (n*12-12+8)){
result8= draw()/pow((1+ytm/12), i8);
i8=i8+12;
price8=price8+result8;
}

while ( i9 <= (n*12-12+9)){
result9= draw()/pow((1+ytm/12), i9);
i9=i9+12;
price9=price9+result9;
}

while ( i10 <= (n*12-12+10)){
result10= draw()/pow((1+ytm/12), i10);
i10=i10+12;
price10=price10+result10;
}

while ( i11 <= (n*12-12+11)){
result11= draw()/pow((1+ytm/12), i11);
i11=i11+12;
price11=price11+result11;
}

while ( i12 <= (n*12-12+12)){
result12= draw()/pow((1+ytm/12), i12);
i12=i12+12;
price12=price12+result12;
}

total_price_without_face_value=price1+price2+price3+price4+price5+price6+price7+price8+price9+price10+price11+price12;

return total_price_without_face_value;

}



int draw(){
return rand() % 12 + 1;
}
Yes, but I think you need to figure it out on your own.

Hint: you can have a loop inside another loop.
thank you, but I tried it and it did not work, any suggestions?
I am not quite good in C++ that is why I use simple codes such as while.
Any help with for loops? I would really appreciate it.
Topic archived. No new replies allowed.