2 Replies - 567 Views - Last Post: 03 October 2014 - 10:47 PM Rate Topic: -----

#1 KairoSinister  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 14

Need help with for loop program

Posted 03 October 2014 - 10:14 PM

So I need to make a for loop for this problem:
A certain type of bacteria doubles its population every twelve hours. If you start with a population of 1000, how many hours will it take for the population to exceed 1,000,000? Output needs to be in table format, such as:
Hours: - Population:
0 ------- 1000
12 ----- 2000
24 ----- 4000

I've created the code, but don't understand how to increment hours by 12 and double the population by 2 each time.
My code:

public class Population
{
    public static void main (String[] args)
    {
        int hours = 0;
        int population;
        for(population = 1000; population <= 1000000; )
        {
            hours = hours + 12;
            population = population * 2;
        }
        System.out.println("Hours\t Population");
        System.out.println("0\t 1000");
        System.out.println("12\t " + population);
        

    }
}

Any help will be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with for loop program

#2 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Need help with for loop program

Posted 03 October 2014 - 10:31 PM

Would you believe your code works?
Here just some changes to let you see it work. I just moved print statement inside and you can also use the operators shown.

		int hours = 0;
		int population;
		for(population=1000;population<=1000000;){
			System.out.println("Hours:"+hours+" Pop:"+population);
			hours += 12;
			population *= 2;
			
		}
		
		System.out.println("Hours:"+hours+" Pop:"+population);


Was This Post Helpful? 1
  • +
  • -

#3 KairoSinister  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 14

Re: Need help with for loop program

Posted 03 October 2014 - 10:47 PM

View Postinfernorthor, on 03 October 2014 - 10:31 PM, said:

Would you believe your code works?
Here just some changes to let you see it work. I just moved print statement inside and you can also use the operators shown.

		int hours = 0;
		int population;
		for(population=1000;population<=1000000;){
			System.out.println("Hours:"+hours+" Pop:"+population);
			hours += 12;
			population *= 2;
			
		}
		
		System.out.println("Hours:"+hours+" Pop:"+population);

Thank you so much. I was getting frustrated with it not working, you helped a lot. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1