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 something like this: they want multiple answers. there is a pattern
322, 321, 320, 319, 318, 312, 311, 310, 300, 299, 298, 297, 296, 295, 288, 287, 286
// 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; } }