Need help writing code for this in command prompt. Instructions to follow: The p
ID: 3546920 • Letter: N
Question
Need help writing code for this in command prompt.
Instructions to follow:
The population of a town A is less than the population of town B. However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of town A will be greater than or equal to the population of town Band the populations of both the towns at that time. (A sample input is: Population of town A = 5,000, growth rate of town A = 4% population of town B = 8,000, and growth rate of town B = 2%.)Explanation / Answer
//in cpp with cin,cout
/*The program needs to tell the user what values it wants (via a message like "Enter population of City A:")
Each time through the loop, the program needs to output the year count and the updated population of both cities
Once the population of city A becomes greater than the population of city B, the loop needs to exit and the program
needs to output the number of years and the final populations of both cities
have your program's while loop stop after 100 years if the population of city A never catches the population of city B. This must be done via a modification to the while loop's logical expression
(can't use "break" for loops)*/
#include<iostream>
#include<string>
using namespace std;
int main(){
int popA,popB;//population
double rateA,rateB;//percentage rates
int years=0;//set year to 0
//get initial populations
cout<<"Enter population of City A: ";
cin>>popA;
cout<<"Enter population of City B: ";
cin>>popB;
//get population growth rates
cout<<"Enter population growth rate of City A (%): ";
cin>>rateA;
cout<<"Enter population growth rate of City B (%): ";
cin>>rateB;
//get into decimal form
rateA/=100.0;
rateB/=100.0;
while(years<100 && popA<popB){//loop until 100 years or popA reaches or execeed popB
years++; //add 1 year
popA*=(1+rateA); //popA grows
popB*=(1+rateB);//popB grows
cout<<"Year "<<years<<", city A has population of "<<popA<<" and city B has population of "<<popB<<endl;
}
cin.get();//get 1 key before closing
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.