fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. // (1)
  7. // input: balance, r, holding_years
  8. // output: balance for each year
  9. int balance = 100, r = 10, holding_years = 10;
  10. for (int i = 1; i <= holding_years; i++) {
  11. balance *= (1 + r / 100.0);
  12. cout << i << ": " << balance << endl;
  13. }
  14.  
  15. // (2)
  16. // input: balance, r, goal
  17. // output: holding_years
  18. balance = 100;
  19. r = 10;
  20. int goal = 200;
  21.  
  22. int n = 0;
  23. while (balance < goal) {
  24. balance *= (1 + r / 100.0);
  25. n++;
  26. }
  27. cout << "Holding years = " << n << endl;
  28. cout << "Balance = " << balance << endl;
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
1: 110
2: 121
3: 133
4: 146
5: 160
6: 176
7: 193
8: 212
9: 233
10: 256
Holding years = 8
Balance = 212