Reputation: 217
for (int j=1; j<=120; j++){
Pop=10180000; //Pop = Population //
Increase=Pop*0.0118;
Pop=Increase+Pop;
cout<< Increase <<endl;
cout<< Pop <<endl;
}
I am really new here, sorry if I made a mistake. I am suppose to find out the amount of population (10.18mil) for 120 months with the increase of 1.18% every month.
I managed to find the first month but my for loop repeats the same result every line for the next 120 lines.
Upvotes: 1
Views: 67
Reputation: 881223
Your problem is that you're setting the initial value of the population every iteration of the loop. You should be doing that once before the loop starts.
You can also simplify the calculation since a 1.18% increase can be achieved by just multiplying by 1.0118
. That gives you something like:
int Pop = 10180000;
for (int i = 1; i <= 120; i++)
Pop = Pop * 1.0118;
cout << Pop << endl;
Of course, if you were writing real code, you may want to factor out the functionality so that it could be re-used easily:
int increaseValue(
int value,
double ratePerPeriod,
unsigned int periodCount
) {
for (unsigned int i = 0; i < periodCount; i++)
value *= (ratePerPeriod / 100.0 + 1.0);
return value;
}
:
cout << increaseValue(10180000, 1.18, 120) << endl;
Upvotes: 1
Reputation: 16
In your code, you re-initialize Pop
as 10180000 at the start of each iteration. You should move it above the loop so that its value does not get reset at each iteration.
Pop=10180000; //Pop = Population //
for (int j = 1; j <= 120; j++) {
Increase=Pop*0.0118;
Pop=Increase+Pop;
cout<< Increase <<endl;
cout<< Pop <<endl;
}
Upvotes: 0