Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

I need help writing a c++ program

OpenStudy (anonymous):

The monthly payment on loan may be calculated by the following formula: Payment = Rate * (1 + Rate)^N ------------------- * L ((1 + Rate)^N - 1) Rate is the monthly interest rate, which is the annual interest rate divided by 12. N is the number of payments and L is the amount of the loan. Write a program that asks for these values an displays a report similar to. Loan amount: $ 10000.00 Yearly Interest rate: 1% Number of Payments: 36 Monthly Payment: $ 332.14 Amount Paid Back: $ 11957.15 Interest Paid: $ 1957.15

OpenStudy (anonymous):

#include <iostream>; #include <iomanip>; #include <string> #include <cmath> using namespace std; int main() { int yearRate, numbPay; double loan, monthPay, paidBack, interest, rate; cout << setprecision(2) << fixed; //gets loan amount, cout << "Enter the loan amount => "; cin >> loan; //gets yearly interest rate cout <<"Enter the yearly interest rate as a percentage => "; cin >> yearRate; //gets number of payments cout << "Enter the number of payments "; cin >> numbPay; //calculates Monthly Payment, Amount Paid back, and Interest Paid rate = yearRate / 12.0 + 1; monthPay = (( rate * pow((1+rate),numbPay)) / ( pow((1+rate),numbPay) - 1 ) ) * loan; paidBack = monthPay * numbPay; interest = paidBack - loan; //outputs the loan, yearRate, numbPay, monthPay, paidBack, and interest variables cout << "Loan Amount: $ " << loan << endl; cout << "Yearly Interest Rate: " << yearRate << "%" << endl; cout << "Number of Payments: " << numbPay << endl; cout << "Monthly Payment: $ " << monthPay << endl; cout << "Amount Paid Back: $ " << paidBack << endl; cout << "Interest Rate: $ " << interest; //this is just to show what rate is for testing purposes cout << "\n\nthis is rate " << rate; //option to end program cin.ignore(); cout << "\nPress enter to end"; cin.get(); return 0; }

OpenStudy (anonymous):

monthPay = (( rate * pow((1+rate),numbPay)) / ( pow((1+rate),numbPay) - 1 ) ) * loan; I believe the error is here

OpenStudy (anonymous):

What is the error?

OpenStudy (anonymous):

the values don't come out there supposed to when I put them in the program

OpenStudy (anonymous):

here is a sample functioning compiled version that the instructor gave for reference

OpenStudy (anonymous):

You should probably have a variable just to store `pow((1+rate),numbPay)` so that you don't call it twice.

OpenStudy (anonymous):

`rate = yearRate / 12.0 + 1;` why are you adding `1` to this twice?

OpenStudy (anonymous):

oh i, i don't now, looks like an error

OpenStudy (anonymous):

fixed it

OpenStudy (anonymous):

the instructor said to make the formula be in one cpp statement

OpenStudy (anonymous):

ok

OpenStudy (anonymous):

what is next?

OpenStudy (anonymous):

well do u know what i mean by getting the wrong values that come out of the program

OpenStudy (anonymous):

Yes

OpenStudy (anonymous):

Is it still not working?

OpenStudy (anonymous):

well run the source and put 5 in all input, then run the exe i posted and put the same input. u get completely different results

OpenStudy (anonymous):

i think im messing up in the line 25 area

OpenStudy (anonymous):

what should you get and what do you get?

OpenStudy (anonymous):

when i put 5 in for the exe i get monthly payment = 1.01, amount paid back = 5.06, and interest paid= .06

OpenStudy (anonymous):

show all output for correct and your program.

OpenStudy (anonymous):

for the source i get 2.53, 12.63, and 7.65, respectively

OpenStudy (anonymous):

the correct is the exe

OpenStudy (anonymous):

Sorry, I'm not going to run some random .exe. I don't trust it.

OpenStudy (anonymous):

who even said I was on windows?

OpenStudy (anonymous):

umm noone, im saying the exe results i posted just now are the right ones

OpenStudy (anonymous):

and the source from my code are incorrect

OpenStudy (anonymous):

What is input?

OpenStudy (anonymous):

it's what i type the console prompt

OpenStudy (anonymous):

.....

OpenStudy (anonymous):

.....

OpenStudy (anonymous):

why is yearly interest rate an `int`?

OpenStudy (anonymous):

because it's a percentage which isn't ussually a fraction for interest, also i don't wasn't there to be two zeros after it like the double's do due to the setprecision(2)

OpenStudy (anonymous):

*want

OpenStudy (anonymous):

But the formula requires you use a decimal, not a percent.

OpenStudy (anonymous):

no, the formula dosen,t even use yearly interest rate, it uses monthly interest rate, which is found using "yearly rate / 12"

OpenStudy (anonymous):

If the yearly rate is 12 for 12%, then monthly rate would be 1, but that is the decimal equivalent of 100%.

OpenStudy (anonymous):

You are confusing the hell out of the computer, because you are giving it a rate that is off by 100.

OpenStudy (anonymous):

dude there are 12 months in a year, the % has nothing to do with the value of the monthly or yearly, it's just a symbol put to show that it's interest

OpenStudy (anonymous):

\[ 1000 \times 50\% \neq 1000 \times 50 \]

OpenStudy (anonymous):

i agree, i still don't see ur point

OpenStudy (anonymous):

Loan amount: $ 5.00 Yearly Interest rate: 5% Number of Payments: 5 Monthly Payment: $ 1.01 Amount Paid Back: $ 5.06 Interest Paid: $ 0.06

OpenStudy (anonymous):

ignore the earlier comment about Loan amount: $ 10000.00 Yearly Interest rate: 1% Number of Payments: 36 Monthly Payment: $ 332.14 Amount Paid Back: $ 11957.15 Interest Paid: $ 1957.15

OpenStudy (anonymous):

i messed up copying from the exe

OpenStudy (anonymous):

just use this as reference for what the program does Loan amount: $ 5.00 Yearly Interest rate: 5% Number of Payments: 5 Monthly Payment: $ 1.01 Amount Paid Back: $ 5.06 Interest Paid: $ 0.06

OpenStudy (anonymous):

Okay let me make it really clear for you.... `int yearRate` `cin >> yearRate;` At this point, `yearRate` has to be an integer like 5 or something. `rate = yearRate / 12.0;` At this point `rate` will be 5/12, which equals about 0.41 `monthPay = (( rate * pow((1+rate),numbPay)) / ( pow((1+rate),numbPay) - 1 ) ) * loan;` At this point, 0.41 is interpreted as 41%, not as 0.41% as it should be.

OpenStudy (anonymous):

That is why your output is so huge.

OpenStudy (anonymous):

well how do u fix it?

OpenStudy (anonymous):

How do you think you fix it? Your number is 100 times too large.

OpenStudy (anonymous):

/100

OpenStudy (anonymous):

so i should fix it when im assigning a value to rate, correct?

OpenStudy (anonymous):

yes

OpenStudy (anonymous):

just hurry up and try it out. divide by 100

OpenStudy (anonymous):

so yearRate/1200 ill see if it works

OpenStudy (anonymous):

HOOOOLLEEEHH SHEEEET, it worked

OpenStudy (anonymous):

thx

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!