Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program is meant to calculate and output population growth
- // information for two different towns based on user input.
- // It takes the population and growth rate of each town
- // and outputs how many years until the population of
- // the smaller town will be greater than the other
- // as well as the population of both towns at that time.
- //**************************************************************
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- int main()
- {
- string townA, townB;
- int popA, popB, lead, year=0;
- double rateA=0.00, rateB=0.00;
- cout << "Please enter the name of the first town"
- << " and press enter: " << endl;
- getline (cin, townA);
- cout << "\nEnter the name of the second town: " << endl;
- getline (cin, townB);
- cout << "\nEnter the population & growth rate for "
- << townA << " separated by space." << endl;
- cin >> popA >> rateA;
- cout << "\nEnter the population & growth rate for "
- << townB << " separated by space." << endl;
- cin >> popB >> rateB;
- if (popA == popB && rateA == rateB)
- {
- cout << "\nBoth populations match & will grow equally."
- << endl;
- cout << townA << " Population: " << popA << " "
- << "Growth Rate: " << rateA << "%\n"
- << townB << "Population: " << popB << " "
- << "Growth Rate: " << rateB << "%\n" << endl;
- system("Pause");
- return 0;
- }
- if (popA == popB && rateA > rateB)
- {
- cout << "\nBoth populations match, but will grow differently."
- << endl;
- cout << townA << " Population: " << popA << " "
- << "Growth Rate: " << rateA << "%\n"
- << townB << " Population: " << popB << " "
- << "Growth Rate: " << rateB << "%\n" << endl;
- system("Pause");
- return 0;
- }
- else if (popA == popB && rateA < rateB)
- {
- cout << "\nBoth populations match, but will grow differently."
- << endl;
- cout << townA << " Population: " << popA << " "
- << "Growth Rate: " << rateA << "%\n"
- << townB << " Population: " << popB << " "
- << "Growth Rate: " << rateB << "%\n" << endl;
- system("Pause");
- return 0;
- }
- if (popB > popA && rateA == rateB)
- {
- cout << "\nAny population disparity will grow indefinitely."
- << endl;
- cout << townA << " Population: " << popA << " "
- << "Growth Rate: " << rateA << "%\n"
- << townB << " Population: " << popB << " "
- << "Growth Rate: " << rateB << "%\n" << endl;
- system("Pause");
- return 0;
- }
- if (popA > popB && rateA > rateB)
- {
- cout << "\nAny population disparity will grow indefinitely."
- << endl;
- cout << townA << " Population: " << popA << " "
- << "Growth Rate: " << rateA << "%\n"
- << townB << " Population: " << popB << " "
- << "Growth Rate: " << rateB << "%\n" << endl;
- system("Pause");
- return 0;
- }
- if (popB > popA && rateB > rateA)
- {
- cout << "\nAny population disparity will grow indefinitely."
- << endl;
- cout << townA << " Population: " << popA << " "
- << "Growth Rate: " << rateA << "%\n"
- << townB << " Population: " << popB << " "
- << "Growth Rate: " << rateB << "%\n" << endl;
- system("Pause");
- return 0;
- }
- if (popA > popB)
- {
- lead = 1;
- }
- do
- {
- popA += (popA * (rateA / 100));
- popB += (popB * (rateB / 100));
- year++;
- }
- while (popA < popB);
- do
- {
- popA += (popA * (rateA / 100));
- popB += (popB * (rateB / 100));
- year++;
- }
- while (popA < popB);
- if (lead == 1)
- {
- cout << "\nThe population of " << townB <<
- " will eclipse " << townA << " in: " << endl;
- cout << year << " year(s).\n";
- cout << "\nAt that time " << townA << "'s population "
- << "will be:\n" << popA << " and "
- << townB << "'s population: " << popB << endl;
- }
- else
- {
- cout << "\nThe population of " << townA <<
- " will eclipse " << townB << " in: " << endl;
- cout << year << " year(s).\n";
- cout << "\nAt that time " << townA << "'s population "
- << "will be:\n" << popA << " and "
- << townB << "'s population: " << popB << endl;
- }
- cin.ignore(2);
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement