In the fall term of 2015, Nick^2 took COMP 2804. Nick was always sitting in the
ID: 3791181 • Letter: I
Question
In the fall term of 2015, Nick^2 took COMP 2804. Nick was always sitting in the back of the classroom and spent most of his time eating bananas. Nick uses the following banana-buying-scheme: At the start of week 0, there are 2 bananas in Nick's fridge. For any integer n greaterthanorequalto 0, Nick does the following during week n: At the start of week n, Nick determines the number of bananas in his fridge and stores this number in a variable x. Nick goes to Jim's Banana Empire, buys x bananas, and puts them in his fridge. Nick takes n + 1 bananas out of his fridge and eats them during week n. For any integer n greaterthanorequalto 0, let B(n) be the number of bananas in Nick's fridge at the start of week n. Determine the value of B(n).Explanation / Answer
Solution Foe the Problem(CPP):-
Program :-
#include<iostream>
using namespace std;
int main()
{
int x=2,i,n;
cout<<"Enter number of weeks(n)"<<endl;
cin>>n;
i=0;
while(i<n) //Counting bananas in each week.
{
cout<<"starting of the week "<<i<<" the "<<x<<" bananas are in nick fridge."<<endl;
for(int j=0;j<7;j++) //In a week 7 days are their because loop repeats 7times.
{
x=x+x; //Nick goes to Jim's banana Empire ,buys x bananas in each day.
}
x=x-(n+1); //Nick eats n+1 bananas in the week.
cout<<"Ending of the week "<<i<<" the "<<x<<" bananas are in nick fridge."<<endl;
i++;
}
return 0;
}
Output :-
Enter number of weeks(n)
2
starting of the week 0 the 2 bananas are in nick fridge.
Ending of the week 0 the 253 bananas are in nick fridge.
starting of the week 1 the 253 bananas are in nick fridge.
Ending of the week 1 the 32381 bananas are in nick fridge.
// Here B(n) value is last week starting bananas = 253.
Note :- If //Nick goes to Jim's banana Empire ,buys x bananas for every day then above code is correct. If //Nick goes to Jim's banana Empire ,buys x bananas only starting day of the week then remove for loop in code just write x=x+x only.
And if you want to print only B(n) then please comment below.
Thank you!