How to do this.

There are 10,000 people in a town whose population increases by 10% each year. Develop a program that determines how many years it would take for the population to exceed 30,000.
Here's a start:
1
2
3
4
5
6
7
const int initial_pop = 10000, target_pop = 30000;
const double rate = 0.1;

int num_years = 0, current_pop = initial_pop;
while( current_pop < target_pop ) {
    // your code here
}
I would have made rate 1.1 instead.

I get

target = initial * pow(1.1,years)

solve that for years, directly, if you want.
A loop is much easier to code. But it isn't required, you can solve it with some logs. Don't forget to round up to the next year if you solve it directly.





Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using std::cout;
int main()
{
    int pop = 10000;
    int year;
    for(year=0;pop<30000;year++)
    {
        pop = pop + pop/10;
    }
    cout << year;
}

Topic archived. No new replies allowed.