Extra Credit #2-Our Town und Suppose there are two towns A and B. The population
ID: 3593735 • Letter: E
Question
Extra Credit #2-Our Town und Suppose there are two towns A and B. The population of town A is less than the population of town B However, the population of town A is growing faster than the population of town B. Task Write a program that prompts the user to enter the population and growth rate of each town. The program outputs the following: after how many years the population of town A will be greater than or equal to the population of town B . the population of both towns at that time Note No one dies in either town. . You need to provide me with your program and 3 sample outputs. You may use the listed example as one of your sample outputs. Sample Output Enter the current population of town A: 5000 Enter the current population of town B: 8000 Enter the growth rate of town A: 4 Enter the growth rate of town B: 2 After 25 year (s) the population of town A will be greater than or equal to the population of town B. After 25 years the population of town A is 13308 After 25 years the population of town B is 13110 Press any key to continue...Explanation / Answer
PopulationOfTownAAndB.java
import java.util.Scanner;
public class PopulationOfTownAAndB {
public static void main(String[] args) {
int townA,townB;
double rateA,rateB,pop1 = 0,pop2=0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("Enter the current population of town A :");
townA=sc.nextInt();
System.out.print("Enter the current population of town B :");
townB=sc.nextInt();
System.out.print("Enter the growth rate of town A :");
rateA=sc.nextInt();
System.out.print("Enter the growth rate of town B :");
rateB=sc.nextInt();
int years=1;
while(true)
{
townA=(int) (townA+townA*(rateA/100));
townB=(int)(townB+townB*(rateB/100));
if(townA>=townB)
{
break;
}
else
{
years++;
continue;
}
}
System.out.println("After "+years+" year(s) the population of town A will be greater than or equal to the population of town B");
System.out.println("After "+years+" years the population of town A is "+townA);
System.out.println("After "+years+" years the population of town A is "+townB);
}
}
__________________
Output:
Enter the current population of town A :5000
Enter the current population of town B :8000
Enter the growth rate of town A :4
Enter the growth rate of town B :2
After 25 year(s) the population of town A will be greater than or equal to the population of town B
After 25 years the population of town A is 13308
After 25 years the population of town A is 13110
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.