The population of 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 B and the populations of both the towns at that time.
REQUIREMENT IDENTIFICATION.
#include <iostream>1. Population of Town A, growth rate of Town A, Population of Town B, growth rate of Town B.
2. Write a program that prompt the user to enter the population and growth rate of each town
3. The population of town A(A) is less than the population of town B(B), (use a for loop and if statement).
4. Include c math library so as to use pow function.
#include <cmath>
using namespace std;
int main() {
int A, B, Ia, Ib;
int n = 1;
float Ra, Rb, Ya, Yb, Za, Zb;
cout << "Enter Population of Town A and B (Population of Town A must be less than Town B)" <<endl;
cin>>Ia>>Ib;
while (Ia <= 0){cout << "Error: Town A must be greater than zero.n Please Enter Population of Town A again.";
cin >> Ia;
}
while (Ib<= 0 || Ib <= Ia)
{cout << "Error: Town B must be greater than zero and greater then Town A. Please Enter Population of Town B again.";
cin >> Ib;
}cout << "Enter Growth rate of Town A and B in Percentage % (growth rate A must be greater than growth rate B)"<<endl;
cin>>Ra>>Rb;
while (Ra <= 0)
{cout << "Error: growth rate A must be greater than zero.n Please Enter growth rate A again.";
cin >> Ra;
}
while (Rb <= 0 ||Rb >= Ra)
{cout << "Error: growth rate B must be greater than zero and less then Town A.n Please Enter growth rate B again.";
cin>> Rb;
}
Ya = (1 + Ra / 100);
Yb = (1 + Rb / 100);
for (;;) {
n++;
Za = pow(Ya, n);
Zb = pow(Yb, n);
A = Ia*Za;
B = Ib*Zb;
{
if (A >= B) {
cout << "The Population of town A After " << n << " years is " << A << endl;
cout << "The Population of town B After " << n << " years is " << B << endl;
break;
cout << endl;}}}
return 0;
}
download Code File here Untitled2.cpp
Comments
Post a Comment