I need help writing a c++ program
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
#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; }
monthPay = (( rate * pow((1+rate),numbPay)) / ( pow((1+rate),numbPay) - 1 ) ) * loan; I believe the error is here
What is the error?
the values don't come out there supposed to when I put them in the program
here is a sample functioning compiled version that the instructor gave for reference
You should probably have a variable just to store `pow((1+rate),numbPay)` so that you don't call it twice.
`rate = yearRate / 12.0 + 1;` why are you adding `1` to this twice?
oh i, i don't now, looks like an error
fixed it
the instructor said to make the formula be in one cpp statement
ok
what is next?
well do u know what i mean by getting the wrong values that come out of the program
Yes
Is it still not working?
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
i think im messing up in the line 25 area
what should you get and what do you get?
when i put 5 in for the exe i get monthly payment = 1.01, amount paid back = 5.06, and interest paid= .06
show all output for correct and your program.
for the source i get 2.53, 12.63, and 7.65, respectively
the correct is the exe
Sorry, I'm not going to run some random .exe. I don't trust it.
who even said I was on windows?
umm noone, im saying the exe results i posted just now are the right ones
and the source from my code are incorrect
What is input?
it's what i type the console prompt
.....
.....
why is yearly interest rate an `int`?
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)
*want
But the formula requires you use a decimal, not a percent.
no, the formula dosen,t even use yearly interest rate, it uses monthly interest rate, which is found using "yearly rate / 12"
If the yearly rate is 12 for 12%, then monthly rate would be 1, but that is the decimal equivalent of 100%.
You are confusing the hell out of the computer, because you are giving it a rate that is off by 100.
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
\[ 1000 \times 50\% \neq 1000 \times 50 \]
i agree, i still don't see ur point
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
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
i messed up copying from the exe
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
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.
That is why your output is so huge.
well how do u fix it?
How do you think you fix it? Your number is 100 times too large.
/100
so i should fix it when im assigning a value to rate, correct?
yes
just hurry up and try it out. divide by 100
so yearRate/1200 ill see if it works
HOOOOLLEEEHH SHEEEET, it worked
thx
Join our real-time social learning platform and learn together with your friends!