0

[SOLVED] I Need Help With This C++ Problem

Students will calculate the balance on a bank credit card that increases at a rate of 2% every month. The initial balance starts at $50. Use a while loop to repeat the increase until the balance reaches $100 or more. By implementing a counter; your program should count the number of months it takes to reach $100 and report it at the end of your program. #include <iostream> using namespace std; int main(){ int accumulate = 100; int balance; double rate = 0.02; int time = 0; double totalbalance = 50; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); while (balance<accumulate) { balance += balance + (balance*2/100); balance -= 50; time += 1; } cout << "This program tells you how long it takes to accumulate a debt of

quot;<< accumulate << ", starting with" << endl; cout << "an initial balance of $50 owed." << endl; cout << "The interest rate is 2%" <<" per month."<<endl; cout << "After " << time << " months," << endl; cout << "your balance due will be
quot; << balance << endl; return 0; }

1st Feb 2022, 6:35 AM
Jevon Harry
Jevon Harry - avatar
4 Answers
+ 3
I think <balance> is better be a `double` cause a 2% increment to an integer *may not* result as expected. Why you decrement <balance> inside the while...loop though?
1st Feb 2022, 8:21 AM
Ipang
+ 1
Alright Good Job! 👍
2nd Feb 2022, 6:00 AM
Ipang
+ 1
#include <iostream> using namespace std; int main() { // your account's balance auto balance = 2452.4; // price for each notebook auto price = 259.99; // Task: calculate the number of notebooks you can afford and output it. // Hint: use an integer to store the result. // Task: calculate the amount left over on your account after the purchase and output it on a new line. // Hint: calculate the total price of the purchase, then substract it from the balance. }
7th Dec 2022, 7:06 PM
Pratik Pawar
Pratik Pawar - avatar
0
Thanks for the answer. At the end, I figured it out. #include <iostream> using namespace std; int main(){ int accumulate = 100; double balance = 50; double rate = 0.02; int time = 0; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); while (balance<accumulate) { balance = balance + (balance*0.02); time++; } cout << "This program tells you how long it takes to accumulate a debt of
quot;<< accumulate << ", starting with" << endl; cout << "an initial balance of $50 owed." << endl; cout << "The interest rate is 2%" <<" per month."<<endl; cout << "After " << time << " months," << endl; cout << "your balance due will be
quot; << balance << endl; return balance; }
2nd Feb 2022, 3:50 AM
Jevon Harry
Jevon Harry - avatar