for loop question

"Mr. Charles has a credit card with a balance of RM100 to settle and has been charged by the bank a 2% monthly interest. Write a program using a repeat/until to calculate how many months can Mr. Charles pass without making any payments before his balance exceeds RM200"

This is what I able to understand and did after learning from this tutorial http://www.cplusplus.com/doc/tutorial/control/

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
 {
 
 	for(float month=100; month<=200;)
 		{
 			month=month*1.02;
     		cout<<month;
 		}
 	return 1;
 }


How to write a program that calculates how many times you loop?
Last edited on
closed account (o3hC5Di1)
Hi there,

Just keep an extra counter in the loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main()
 {
        float month;
        int i;
 	for( month=100,  i=1; month<=200; i++)
 		{
 			month=month*1.02;
     		cout<<month;
 		}

        std::cout << i << " months can pass.";
 	return 1;
 }


On a sidenote: you should return 0 from main when the program is successful.

Hope that helps.

All the best,
NwN
Last edited on
Thank you so much! :D
repeat/until in C++ would be a do-while loop.

It would be, in outline something like this:

1
2
3
4
5
6
7
   
    int balance = 100;
    int month = 0;
    do {
       // additional code required here
        month++;    
    } while (balance < 200);


Inside the loop you need to increase the balance by 2%.
You might instead want to work using pennies or cents, and thus use 10000 and 20000 as the figures. It may not affect the outcome, but you might also need to consider rounding the figure rather than truncating it each time,
Last edited on
Topic archived. No new replies allowed.