Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int input(float*, int*, float*);
float calculation(float, int, float, int, float*, float*, int, float*);
float subCalculation(float, float, float);
int main(void)
{ // variable declaration
float bondPrice;
int initBond;
float intRate;
int numYear;
float value;
float annualPay;
float amtRetired;
float interest;
int N;
N = 0;
//function call to input
numYear = input(&bondPrice, &initBond, &intRate);
printf("YEAR VALUE INTEREST PAYMENT AMT RETIRED\n");
for(N = 0; numYear - N >= 0; N++)
{
annualPay = calculation(bondPrice, initBond, intRate, numYear, &value, &interest, N, &amtRetired);
printf("%d %8.2f %8.2f %8.2f %8.2f\n", N, value, interest, annualPay, amtRetired * bondPrice);
}
system("PAUSE");
return 0;
}
//main
////////////////////////////////////////////////////////////////////////////////
int input(float* price, int* bond, float* interestRate)
{
// variable declaration
int years;
//prompt user input for all listed in function "Pre"
printf("Please enter price per bond: ");
scanf("%f", price);
printf("\nPlease enter the initial number of bonds: ");
scanf("%d", bond);
printf("\nPlease enter the interest rate: ");
scanf("%f", interestRate);
printf("\nPlease enter the number of years the debt is to be paid: ");
scanf("%d", &years);
//return years
return years;
}
//input
////////////////////////////////////////////////////////////////////////////////
float calculation(float price, int bond, float interestRate, int years, float* value,
float* interest, int N, float* amtRetired)
{ // variable declaration
int r;
float payment;
//calculate value of bond
*value = (float)price * bond;
*interest = (float)(interestRate/100) * *value;
payment = (*value * (interestRate/100))/(1 -(1/pow(1 + (interestRate/100), years - N)));
*amtRetired = subCalculation(price, payment, *interest);
// return payment
return payment;
}
//calculations
////////////////////////////////////////////////////////////////////////////////
float subCalculation(float price, float payment, float interest)
{ // variable declaration
float amtRetired;
amtRetired = (payment - interest)/(price);
amtRetired = floor(amtRetired +.5);
return amtRetired;
}