The U.S. Census Bureau projects population based on the following assumptions: T
ID: 3674053 • Letter: T
Question
The U.S. Census Bureau projects population based on the following assumptions:
There is one birth every 7 seconds
There is one death every 13 seconds
There is one new immigrant every 45 seconds
Using program design language, write an algorithm that a program can use to calculate and display the population for each of the next five years. You must post your solution in the discussion topic thread.
You may assume that the current population is 312,032,543 and that a year has 365 days. Your algorithm must prompt users for the current population, and the birth, death, and immigration rates. Use Java comments to annotate your algorithm.
Explanation / Answer
import java.util.Scanner;
public class PopulationCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long curent_pop = 312032543;
int birthRate;
int immigrantRate;
int deathRate;
// taking user inputs
System.out.print("Enter current population: ");
curent_pop = sc.nextLong();
System.out.print("Enter birth rate: ");
birthRate = sc.nextInt();
System.out.print("Enter death rate: ");
deathRate = sc.nextInt();
System.out.print("Enter immigrant rate: ");
immigrantRate = sc.nextInt();
sc.close();
// number of seconds in a year of 365 days
long seconds = 60*60*24*365;
//iterating over 5 years
for(int i=1; i<=5; i++){
long deathPerYear = seconds/deathRate;
long birthPerYear = seconds/birthRate;
long immigrantPerYear = seconds/immigrantRate;
//calculating current population
curent_pop = curent_pop + birthPerYear + immigrantPerYear -deathPerYear;
System.out.println("Population at end of "+i+" year: "+curent_pop);
}
}
}
/*
Output:
Enter current population: 12345677
Enter birth rate: 12
Enter death rate: 13
Enter immigrant rate: 50
Population at end of 1 year: 13178551
Population at end of 2 year: 14011425
Population at end of 3 year: 14844299
Population at end of 4 year: 15677173
Population at end of 5 year: 16510047
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.