Answer:
Below is the required code:
Explanation:
Using for loop
#include <iostream>
using namespace std;
int main()
{
//Initial crud size
int init = 0;
int newCrud;
int next=0;
//Number of days to simulate
int no_days;
int day;
cout << "Enter initial amount of green crud: ";
cin >> newCrud;
cout << "Enter number of days to simulate: ";
cin >> no_days;
for (day = 10; day<=no_days; day++)
{
if (day % 10 == 0)
{
next = newCrud + init;
}
newCrud = init;
init = next;
}
if (no_days < 5)
cout << "\nCrud reproduce only after 5 days minimum.Hence the current amount is "
<< newCrud << " pounds.";
else
cout << "On day " << no_days << " you have " << init
<< " pounds of green crud." << endl;
cout << "\nWould you like to continue? (y or n): ";
cin >> ans;
return 0;
}
Output:
Enter initial amount of green crud: 5
Enter number of days to simulate: 220
On day 220 you have 10485760 pounds of green crud.
Using while loop
Program:
#include <iostream>
using namespace std;
int main()
{
char ans='y';
while (ans == 'Y' || ans == 'y')
{
//Initial crud size
int init = 0;
int newCrud;
int next=0;
//Number of days to simulate
int no_days;
int day;
cout << "Enter initial amount of green crud:
";
cin >> newCrud;
cout << "Enter number of days to simulate:
";
cin >> no_days;
for (day = 10; day<=no_days; day++)
{
if (day % 10 == 0)
{
next = newCrud + init;
}
newCrud = init;
init = next;
}
if (no_days < 5)
cout << "\nCrud reproduce only after 5 days
minimum.Hence the current amount is "
<< newCrud << " pounds.";
else
cout << "On day " << no_days << " you have "
<< init
<< " pounds of green crud." << endl;
cout << "\nWould you like to continue? (y or
n): ";
cin >> ans;
}
return 0;
}
Output:
Enter initial amount of green crud: 5
Enter number of days to simulate: 220
On day 220 you have 10485760 pounds of green crud.
Would you like to continue? (y or n): y
Enter initial amount of green crud: 5
Enter number of days to simulate: 225
On day 225 you have 10485760 pounds of green crud.
Using do while loop
Program:
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
//Initial crud size
int init = 0;
int newCrud;
int next=0;
//Number of days to simulate
int no_days;
int day;
cout << "Enter initial amount of green crud: ";
cin >> newCrud;
cout << "Enter number of days to simulate: ";
cin >> no_days;
for (day = 10; day<=no_days; day++)
{
if (day % 10 == 0)
{
next = newCrud + init;
}
newCrud = init;
init = next;
}
if (no_days < 5)
cout << "\nCrud reproduce only after 5 days
minimum.Hence the current amount is "
<< newCrud << " pounds.";
else
cout << "On day " << no_days << " you have " <<
init << " pounds of green crud." << endl;
cout << "\nWould you like to continue? (y or n):
";
cin >> ans;
} while (ans == 'Y' || ans == 'y');
return 0;
}
Output:
Enter initial amount of green crud: 5
Enter number of days to simulate: 220
On day 220 you have 10485760 pounds of green crud.
Would you like to continue? (y or n): y
Enter initial amount of green crud: 5
Enter number of days to simulate: 225
On day 225 you have 10485760 pounds of green crud.