Read in two variables, year and population. Each year the population increases b
ID: 641608 • Letter: R
Question
Read in two variables, year and population. Each year the population increases by 10%. Write a program that displays the annual population and stops in the year when the town will count over 100,000 people.
Make sure that the population is positive, non-zero.
For example:
What I tried:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
float i=0.10;
int n=0;
int p;
int Year;
float Pop;
scanf("p=%d year=%d ", &p, &Year);
while (Pop<=100000 && Pop>0){
Pop=p*pow((i+1),n++);}
printf("%f",Pop);
if(Pop>0){
printf("Year Population ------------------ ");
printf("%d %.2f ", Year, Pop);
}
else if (p<=0)
printf("Population must be larger than 0");
return 0;
}
------------------
2013 87650
2014 96415 p=-4 year=2013 Population must be larger than 0
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int p;
int year;
printf("Enter population: ");
scanf("%d", &p);
while(p <= 0){
printf("Population can not be negative. Enter again! ");
printf("Enter population: ");
scanf("%d", &p);
}
printf("Enter year: ");
scanf("%d", &year);
while(p < 100000){
year++;
p += (p * 0.1); // (p * 0.1) is 10 percent of the current population
}
printf("Population has crossed 100000 ");
printf("Population in %d is %d ", year, p);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.