Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am writing a program to show a population growth for a town over 5 years at a 4% growth rate.
I have my variables and my counter. Here is what I have.
static void Main(string[] args)
      {
          int curntPopulation = 23962;
          int i = 1;
          double growthRate = 1.04;
          double newPopulation;


          while (i <= 5)
          {
              newPopulation = curntPopulation * growthRate;

              newPopulation = newPopulation * growthRate;

              Console.WriteLine("The population of Davidville is: {0}", newPopulation);
              i++;
          }

      }


What I am having difficulties with is displaying the new population for each of the 5 years, currently I am displaying the year two results for all 5 lines. How can I make newPopulation save the current value, store it in the equation and generate the result I am after? Thanks
Posted
Updated 9-Aug-13 12:11pm
v2
Comments
Richard C Bishop 9-Aug-13 18:14pm    
Your issue is not clear, but why are you multiplying growthRate twice?

1 solution

C#
static void Main(string[] args)
        {
            int population = 23962;
            int totalNumberOfYears = 5;
            double growthRate = 1.04;

    Console.WriteLine("The population of Davidville is: {0}", newPopulation);

    for(int year = 1; year < totalNumberOfYears; year++)
    {
        population = population * growthRate;

        Console.WriteLine("The population of Davidville in year {0} is: {1}", year, newPopulation);
    }

}


Try that, it should do the job.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900