Answer:
Complete c++ code with explanation and output results is given below
Step-by-step explanation:
Fibonacci numbers are defined as F0 = 1, F1 = 1, and Fi + 2 = Fi + Fi + 1 where i = 0, 1, 2, 3,... it means that each number is the sum of previous two numbers.
Fibonacci numbers has application in population growth rates such as green crud population.
Initial size of population is taken from the user and also the number of days for population growth.
If the user input negative population or days is a negative number then error message is shown and program terminates.
the growth constant is set to 10 days meaning that population will grow after every 10 days. rate is the number of times population will grow then Fibonacci numbers are initialized and a for loop is used to compute the Fibonacci numbers that will determine the crud population growth.
Code:
#include <iostream>
using namespace std;
int main()
{
int temp = 10;
int days;
int rate;
float initial_size;
cout << "Please enter the initial weight (in pounds) of the population: "<<endl;
cin >> initial_size;
if(initial_size < 0)
{
cout<<"Error! Invalid input"<<endl;
exit(0);
}
cout << "Enter the number of days "<<endl;
cin >> days;
if(days < 0)
{
cout<<"Error! Invalid input"<<endl;
exit(0);
}
double f0 = 0;
double f1=initial_size;
double f2=f0+f1;
for(int i = 0 ; i < rate ; i++)
{
// Fibonacci sequence
f2= f1 + f0;
f0 = f1;
f1 = f2;
}
cout <<"Green Crud Population has grown to: "<< f2 << " pounds.";
return 0;
}
Output:
Please enter the initial weight (in pounds) of the population:
100
Enter the number of days
50
Green Crud Population has grown to: 800 pounds.
Please enter the initial weight (in pounds) of the population:
-50
Error! Invalid input