In: Computer Science
Assume that the population of Mexico is 128 million and that the population increases 1.01 percent annually. Assume that the population of the United States is 323 million and that the population is reduced 0.15 percent annually. Write an application that displays the populations for the two countries every year until the population of Mexico exceeds that of the United States, and display The population of Mexico will exceed the U.S. population in X years
The answer should look like this for Mexico:
322 321 320 319 318 312 311 310 300 299 298 297 296 295 288 287 286
This the answer for USA:
129 133 137 140 144 147 150 153 156 158 161 166 248 256 269 280
SOLUTION :
CONSIDERING THE CONDITIONS AND REQUIREMENTS FROM THE QUESTION
HERE CODE:
// Java solution for the above problem statement
import java.math.*;
import java.util.*;
class Population {
public static void main (String[] args) {
int mexico_population=128; // in million
double population_increase_rate=1.01; // in percent for
Mexico
int us_population=323; // in million
double population_decrease_rate=0.15 ; // in percent for US
int n=1;
while( true )
{
int
mexico=Mexico_population(mexico_population,population_increase_rate,n);
int
us=US_population(us_population,population_decrease_rate,n);
if(mexico<us) {
if (n == 1) {
} else {
System.out.print(", ");
}
}
else {
System.out.println();
System.out.println("Number of years taken by Mexico to exceed US :
" + n);
break;
}
n++;
System.out.print(mexico);
}
}
public static int Mexico_population(int initial_pop,double rate,int
n){
int ans= (int)(initial_pop*(Math.pow((1 + (rate*0.01)),n)));
return ans;
}
public static int US_population(int initial_pop,double rate,int
n){
int ans= (int)(initial_pop*(Math.pow((1 - (rate*0.01)),n)));
return ans;
}
}
NOTE : PLEASE UPVOTE ITS VERY MUCH NECESSARY FOR ME A LOT. PLZZZZ......