Wednesday, July 1, 2015

// // 4 comments

Java World Population Growth Calculator

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 2 Exercise 2.34.

Question: Use the web to determine the current world population and the annual world population growth rate. Write an application that inputs these values, then displays the estimated world population after one, two, three, four and five years.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.34 - World Population Growth Calculator
 * This Program Calculates And Displays The Estimated World Population After One, Two, Three, 
 * Four And Five Years
 *
 */

//Based on data from worldometer World Population clock (worldometers.info/world-population)
//Geohive (www.geohive.com/earth/population_now.aspx)
//US World Population Clock (www.census.gov/popclock)
//Current world population is 7,321,870,923 with an average yearly growth rate of 1.10% 

import java.util.Scanner;

public class Ex02_34 {
    public static void main (String [] args) {

 Scanner input = new Scanner (System.in);
        
        double currentPop; 
        double growthRate;
        double annualPopIncrease;
        double estimatedPop;
        
        System.out.print ("Enter Current World Population: ");
        currentPop = input.nextLong();
        System.out.print ("Enter Annual Population Growth Rate: ");
        growthRate = input.nextInt();
        annualPopIncrease = (growthRate / 100) * currentPop;
        estimatedPop = currentPop + annualPopIncrease;

        System.out.println ();

        System.out.println("Estimated population after one year:     " + estimatedPop);
        System.out.println("Estimated population after two years:    " + estimatedPop + (annualPopIncrease * 2));
        System.out.println("Estimated population after three years:  " + estimatedPop + (annualPopIncrease * 3));
        System.out.println("Estimated population after four years:   " + estimatedPop + (annualPopIncrease * 4));
        System.out.println("Estimated population after five years:   " + estimatedPop + (annualPopIncrease * 5));

    }
}

Click here to see other answers to Java How to Program.

4 comments:

  1. not working broda

    ReplyDelete
    Replies
    1. Thanks for pointing it out. It had input mismatch but it's been corrected and now works fine.

      Delete
  2. The workig data estimated pop * number of years is wron i Think as estimatedpop becomes after one year but multiplying estimatedpop by 2 for the population after two years is mathematical problem which staes that growth rate was 200%. Instead it should be estimatedpop + growth/100 * estimated pop to be precise but if it was just a simple calculation you need to multiply the annualpopincrease by 2 and add it to estimatedpop.

    I do not mean to be rude and also THANKS A LOT FOR PROVIDING THE SOLUTIONS IT IS HELPING ME A LOT.

    ReplyDelete
    Replies
    1. Thank you so so much for the correction. Didn't think of it that way - logic error! It has been corrected.

      P.S. I'm glad you find it helpful.

      Delete


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.