The town of Rien, Ohio currently has a population 9,870. Due to the recent boom in craft breweries in the town, the population has been growing at a rate of 10% per year. The city fathers are concerned about what will happen when the town reaches 30,000 people and the strain that this type of population will put on the public utilities etc.
The loop here is a conditional loop. We will allow the population to continue to expand until it reaches the target operation. We are also going to write our nicely formatted output to a file, which we will be sending to the town council.
You immediately realize that if you made the program more general, allowing a community to enter their current population, their "target" population and a project growth rate, they could also use this software. So you are to have the user enter the current year, the current population, target population and the growth rate. It will then display the population for each year, terminating when the target population has been reached or exceeded. (Note that population growth is like compound interest, the interest is computed as a percentage of the larger population at the end of each year:
pop = pop + pop*growth_r;)
We will also declare a string variable (char town[20]; )and use the %s format placeholder to read into it and output. (Note that when you do a scanf with a string you do NOT use the & before the variable name. )
The output looks something like this: {Note that values in bold have been entered by the user.}
What is the name of your town? Rien
Begin by entering the current year => 2020
Now enter your town's current population => 9870
Enter the expected population growth as a percentage => 10
Population Projection for Rien:
Now enter the target population => 30000
2020 9870
2021 10857
2022 11942
2023 13136
2024 14449
2025 15893
2026 17482
2027 19230
2028 21153
2029 23268
2030 25594
2031 28153
In the year 2032 you will exceed your target of 30000 with a population of 30968
Now have the same output printed out to a file called "report.txt" You can have it print to both the screen and to the file by putting, underneath each printf, an fprintf command the echoes the printf. (Remember that you will have to declare a FILE* variable, and you will have to open the file.)