Question: C++ programming question. I did the codes but my year result is wrong. Please help me fix this. Question: The population of a town A

C++ programming question. I did the codes but my year result is wrong. Please help me fix this.

Question: The population of a town A is less than the population of a 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 B and the populations of both towns at that time.

My codes:

#include

using namespace std; int townPopulationA, townPopulationB, year; double townGrowthRateA, townGrowthRateB, finalA, finalB;

int main() { cout<<"This program is to compare population growth. Town A population is less than Town B.But Town A have higher growth rate "; cout << "Please enter higher population for town B but higher increase for town A ";

cout << "Enter the population of Town A: "; cin >> townPopulationA;

cout << "Enter the population of Town B: "; cin >> townPopulationB;

if (townPopulationA < townPopulationB) { cout << "Enter the growth rate of town A: "; cin >> townGrowthRateA; cout << "Enter the growth rate of town B: "; cin >> townGrowthRateB; if (townGrowthRateA <= townGrowthRateB) { cout << "Your input is invalid"; return 1; } else if (townGrowthRateA > townGrowthRateB) { finalA = townPopulationA * (1 + townGrowthRateA / 100.00); finalB = townPopulationB * (1 + townGrowthRateB / 100.00); year++;

cout << "Town A Population: " << finalA << endl; cout << "Town B Population: " << finalB << endl; } cout << "After " << year << " year, the population of town A is greater or equal to town B "; } else if (townPopulationA > townPopulationB) { cout << "Your input is invalid"; return 1; } system("pause");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!