In: Computer Science
C++
Instructions
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 program outputs:
(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%.)
Hey mate, find the code for above program below. And do refer the screenshot for proper indentation and error free execution of the code.
PROGRAM -
#include<iostream>
using namespace std;
int main()
{
    int popA,popB,year=0;    /* integer variables to store population of town A and B 
                                and assigning year=0 to take it as present year */
    
    float grA,grB;           /* float variables to store growth rate of town A and B */
    
    cout<<"The population of town A : ";
    cin>>popA;
    cout<<"The growth rate of town A in % : ";
    cin>>grA;
    cout<<'\n';
    
    cout<<"The population of town B : ";
    cin>>popB;
    cout<<"The growth rate of town B in % : ";
    cin>>grB;
    cout<<'\n';
    
    if(popA < popB && grA > grB)
    {
        while(popA < popB)
        {
            popA=((grA/100)*popA)+popA;  /* calculating population growth of town A in one year */
            popB=((grB/100)*popB)+popB;  /* calculating population growth of town B in one year */
            year=year+1;                 /* incrementing year until population of town A is greater than town B */
        }
        
        if(popA > popB){
            cout<<"After "<<year<<" years, the population of town A will be greater than population of town B\n";
        }
        else{
            cout<<"After "<<year<<" years, the population of town A will be equal to population of town B\n";
        }
        cout<<"Population of town A after "<<year<<" years : "<<popA<<'\n';
        cout<<"Population of town B after "<<year<<" years : "<<popB;
    }
    
    else
    {
        cout<<"Invalid input due to one of the following reasons:\n";
        cout<<"1.Population of town A is already greater than or equal to town B\n";
        cout<<"2.Growth rate of town A is less than town B\n";
    }
    
    return 0;
}

SAMPLE OUTPUT -

