Hello all. I am trying to write a program that would allow me to find the highest interest rate I could afford based off of length of the loan, amount borrowed and the most I could pay a month. My problem is that I don't know how to continually increase the interest fractionally until I reach the desired number. I know that if I do ++ it continually increases by 1 but I need to increase continually by only .001. Also, when this number is reached, how do I display it? Lastly, an exact number might not be able to be reached (i.e. .0625 is too small but .0626 is too large). If this is the case, I would need it to display the largest number without going over (i.e. .0625 from the example above).
Now that I write this, it seems like it is probably more complicated than I had originally thought. Here is the code that I have so far:
#include <cstdlib>
#include <iostream>
usingnamespace std;
#include <math.h>
int main(int argc, char *argv[])
{
float d;
float a;
float B;
cout<<"How many years is the loan for? ";
cin>>d;
cout<<"How much do you want to borrow? ";
cin>>a;
cout<<"What is the most you will pay a month? ";
cin>>B;
float i=d*12;
float r=.001;
float b=r/12;
float c=b+1;
float j=pow(c,i);
float f=1/j;
float g=1-f;
float h=g/b;
float z=a/h;
while (z<B)
{
;
}
cout<<"The highest rate you can afford is "<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
I left blank the spots where I think the code should go. I apologize for what is probably sloppy code, I wrote this after a day of reading tutorials and only made it as far as functions and also from what I remembered from reading tutorials from a year or more ago.
One option for getting an increment of 0.001 is to have code such as
1 2 3 4 5 6 7
double interestRate;
for (int i=60; i<70; i++) //Rates from 0.060 to 0.070
{
interestRate = double(i)/1000; //case i to double otherwise calculation will
//use integer arithmetic, return 0 then convert to double:-(
//calculation using rate here
}
To stop when you have found the limit you want there are a couple of options.
You could have multiple conditions in the for loop
1 2
for (int i=60; i<70 && monthlyPayment<maxICanAfford ; i++)
...
or you can use a break statement to jump out of a for loop.
To get the value you want you can use a second set of varaibles
1 2 3 4 5 6 7 8 9 10 11 12 13
for ...
{
//do calculations
if (monthlyPayment<maxICanAfford)
{
//copy values to second set
}
else
{
//exit loop
}
}
//Print second set