1. Member Join Date
Oct 2009
Posts
1
Rep Power
0

## Java problem help!

Ok so I am studying Java for the first time and am really stuck on the following program.

The population of a town is now at 15000 and it is expanding at 5% per year . Write a program to determine how many years it will take for the population to exceed 85000.

I understand mathematically how you get the answer but not in java.

Below is my lousy attempt :)

import java.util.*;
population
{

//The population of a town is now at 15000 and it is expanding at 5% per year . Write a program to determine how many years it will take for the population to exceed 85000.

public static void main(String[] args)

{
Scanner sc = new Scanner(System.in);
// variables

double population, year1, year2, yearn;
final DOUBLE ENDPOP = 85000;

// initialise loop control variable

while (population < ENDPOP)

{
// year 1 population increase

System.out.println("enter current population");
population = sc.nextDouble():

year1 = (population * 1.05);
year2 = (year1 * 1.05);
yearn = (year(n-1) * 1.05);

} // end while

System.out.println("Number of years for population to exceed 85000 is " + ?);
}// end main

}// end class  Reply With Quote

2. Senior Member Join Date
Aug 2009
Posts
2,388
Rep Power
13

## How would you get the answer mathematically? Write down those steps.
Then convert the steps to a Java program.  Reply With Quote

3. Member Join Date
Aug 2009
Posts
1
Rep Power
0

## class Population
{
public static void main(String args[])
{
double intialpopulation=15000D;
double finalpopulation=85000D;
double percentage=5;
double yearlyIncreaseinpoulation=(percentage/100)*intialpopulation;
System.out.println("yearlyIncreaseinpoulation"+yea rlyIncreaseinpoulation);
double noofyears=(finalpopulation-intialpopulation)/yearlyIncreaseinpoulation;
System.out.println(noofyears);
}
}  Reply With Quote

4. ##  Originally Posted by suganya_prasad class Population
{
public static void main(String args[])
{
double intialpopulation=15000D;
double finalpopulation=85000D;
double percentage=5;
double yearlyIncreaseinpoulation=(percentage/100)*intialpopulation;
System.out.println("yearlyIncreaseinpoulation"+yea rlyIncreaseinpoulation);
double noofyears=(finalpopulation-intialpopulation)/yearlyIncreaseinpoulation;
System.out.println(noofyears);
}
}
That most certainly isn't true: if you start with a population P(0) and the growth is i percent per year, then after n years the population P(n) is: (1+i/100)^n*P(0); if you have P(0) == 15000 and P(n) == 85000 then a bit of 101 math tells you: 85000 ==1.05^n*15000 --> 85000/15000= 1.05^n --> n == 35.5... little babies grow up and start making little babies too you know.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•