-
don't get this problem??
Hi im having problem with this exercise i want it to do it all in one for loop this is very easy but i can't figure it out need help there:{...
here's the question
Credit Card Bill
Say that you owe the credit card company $1000.00. The company charges you 1.5% per month on the unpaid balance. You have decided to stop using the card and to pay off the debt by making a monthly payment of N dollars a month. Write a program that asks for the monthy payment, then writes out the balance and total payments so far for every succeeding month until the balance is zero or less.
Enter the monthly payment:
100
Month: 1 balance: 915.0 total payments: 100.0
Month: 2 balance: 828.725 total payments: 200.0
Month: 3 balance: 741.155875 total payments: 300.0
Month: 4 balance: 652.273213125 total payments: 400.0
Month: 5 balance: 562.057311321875 total payments: 500.0
Month: 6 balance: 470.4881709917031 total payments: 600.0
Month: 7 balance: 377.54549355657866 total payments: 700.0
Month: 8 balance: 283.20867595992735 total payments: 800.0
Month: 9 balance: 187.4568060993263 total payments: 900.0
Month: 10 balance: 90.26865819081618 total payments: 1000.0
Month: 11 balance: -8.377311936321576 total payments: 1100.0
For each month, calculate the interest due on the unpaid balance. Then calculate the new balance by adding the interest and subtracting the payment.
Improved Program: Have the program prompt for the beginning balance, the monthly interest, and the payment amount. Also, when the balance falls below the amount of the monthly payment, write out the final payment that will bring the balance to exactly zero.
and here's what i code...
Code:
#include <iostream>
using namespace std;
int main()
{
float mp,chrg;
cin>>mp;
chrg=1000*1.5/mp;
for(int x=1000;x>=11;x=x-mp+chrg){
cout<<x<<"\n";
}
cin.get();
}
how can i do this in one for loop plss help:{...tnx....
-
How do you do it by hand? For math problems you should define the steps first instead of start writing code. Then you can put them in an algorithm.
-
the code you have is way too simplistic. avoid using static values (like 1000) make everything variables from the start.
Initialise all variables when they are declared (or at least before you use them)
e.g. float mp=0.0f,chrg=0;
the interest charge needs to be recalculated every loop on the remaining balance.
There are several ways to do this code, here's one suggestion
Code:
float mp=0,chrg=0;
float balance=1000;
float interest_rate=1.5;
float total_payments=0;
cin>>mp;
for(;;){
// add 1.5% to balance
balance+=(balance/100)*interest_rate;
// calculate new balance after payment
balance-=mp;
// recalculate total payments
total_payments+=mp;
// output new balance
cout<<balance<<"\n";
// validate new balance
if(balance<=0) break;
}
cout <<"End.\n";
as for the improved program...
you need to test when a monthly payment will take the balance below zero, break there instead of subtracting, and work out the amount to pay on the last installment to bring the balance to zero.
As this is an excercise i've resisted the temtation to write the whole thing properly. You won't learn anything if people do it for you....
-
On a seperate note, try to format your code so it is a little easier to read.
Ie:
Code:
cout<<"This is a strinng\n";
is harder to read than
Code:
cout << "This is a string\n";
I guess it is all down to personal taste, but well presented code is crucial to understanding a
problem