Code taking a lot of time but no result

OK so there is this question:

The country A has 50M inhabitants, and its population grows 3% per year. The country B, 70M and grows 2% per year. Tell in how many years A will surpass B.


And the heading specifies my problem.

Finally here is my code (yes it is C):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main()
{
	const int A_GROWTH_RATE = 3;
	const int B_GROWTH_RATE = 2;
	const int A_POPULATION = 50; //MILS
	const int B_POPULATION = 70; //MILS
	
	float a_pop_curr = A_POPULATION;
	float b_pop_curr = B_POPULATION;
	
	int years = 0;
	
	while(a_pop_curr <= b_pop_curr)
	{
		a_pop_curr += a_pop_curr*(A_GROWTH_RATE/100);
		b_pop_curr += b_pop_curr*(B_GROWTH_RATE/100);
		years++;
	}
	
	printf("After %d years, A will have a population of %.2f while B will have a population of %.2f", years, a_pop_curr, b_pop_curr);
	
	return 0;
}
Last edited on
Integer division

These resolve to 0.
(A_GROWTH_RATE/100); = 0
(B_GROWTH_RATE/100); = 0

Last edited on
OK I changed those lines and now it works! Thanks :D!
1
2
a_pop_curr += a_pop_curr*(A_GROWTH_RATE/100.0);
b_pop_curr += b_pop_curr*(B_GROWTH_RATE/100.0);
Topic archived. No new replies allowed.