Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Help with a program (beginner)

1. ## Help with a program (beginner)

I have a question for one of my high school programming projects and i'm not really sure where to start. Here's the problem.

"Mexico's population is 62 million and is growing at the annual rate of 7%. The United States' current population is 280 million and is growing at the annual rate of 2%. If these two countries were to maintain their current rates of growth, in how many years will Mexico's population be more than half that of the United States? Your program should answer this question."

I decided to make it so it would run Mexico's population * 1.07^years and USA's pop * 1.02^years

but when I run my program it says that Mexico's pop. catches up to half of the USA's in 1 year which is definitely not true. (the answer is 18 years)

Any help is appreciated!

```import java.util.Scanner;
public class h3q3 {
public static void main (String [] args){
Scanner scan = new Scanner (System.in);

double mex = 62000000;
double usa = 280000000;
double years = 0;

while(mex <= usa/2)
{
years++;
mex = mex * Math.pow(1.07, years);
usa = usa * Math.pow(1.02, years);
if (mex > (usa/2));{
break;
}
}
System.out.println ("Mexicos population is half of America's in " + years + "years ");
}
}```

2. ## Re: Help with a program (beginner)

Try computing the values manually for each year for a couple of years and writing the values on a piece of paper. One line for each year.
Add a println statement to the code to print out the same values that were manually computed for each year.
Compare the values.
Change the program so it computes the same values for a year as were manually computed.

3. ## The Following User Says Thank You to Norm For This Useful Post:

gluecows (March 6th, 2013)

4. ## Re: Help with a program (beginner)

I did what you said. It took some time but I figured out my issue and got the program to work. Thanks!

5. ## Re: Help with a program (beginner)

You're welcome.

6. ## The Following User Says Thank You to Norm For This Useful Post:

gluecows (March 6th, 2013)