[try Beta version]
Not logged in

 
How to do this.

Mar 25, 2017 at 6:28am
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.
Mar 25, 2017 at 7:35am
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
}
Mar 25, 2017 at 3:40pm
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 Mar 25, 2017 at 3:42pm
Mar 25, 2017 at 7:47pm
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.