Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A Teddy Bear Picnic This question involves a game with teddy bears. The game sta

ID: 3801601 • Letter: A

Question

A Teddy Bear Picnic
This question involves a game with teddy bears. The game starts when I give you some bears.
You can then give back some bears, but you must follow these rules (where n is the number
of bears that you have):
If n is even, then you may give back exactly n/2 bears.Hint: To test whether n is even,
use the expression ((n % 2) == 0).
If n is divisible by 3 or 4, then you may multiply the last two digits of n and give
back this many bears. (By the way, the last digit of n is n%10, and the next-to-last digit
is ((n%100)/10).
If n is divisible by 5, then you may give back exactly 42 bears.
The goal of the game is to end up with EXACTLY 42 bears.
For example, suppose that you start with 250 bears. Then you could make these moves:
--Start with 250 bears.
--Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears.
--Since 208 is even, you may return half of the bears, leaving you with 104 bears.
--Since 104 is even, you may return half of the bears, leaving you with 52 bears.
--Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and
return these 10 bears. This leaves you with 42 bears.
--You have reached the goal!
Write a recursive function to meet this specification:
// Postcondition: A true return value means that it is possible to win
// the bear game by starting with n bears. A false return value means that
// it is not possible to win the bear game by starting with n bears.
// Examples:
// bears(250) is true (as shown above)
// bears(42) is true
// bears(84) is true
// bears(53) is false
// bears(41) is false
*/
bool bears(int n);

Explanation / Answer

#include <stdio.h>
int main()
{
int number;
int results;
printf("Enter an integer: ");
scanf("%d", &number);
if(number<0 || number==0 || number<42)
{
printf("You have not reached the goal");
return ;
}
results=bears(number);
if (results==1)
{
printf("You have reached the goal");
}
else
{
printf("You have not reached the goal");
}
}
  


int bears(int n)
{
int result;
int res;
int rem;
int dividend;
if(n==42)
{
result=1;
return result;
}
  
else if (n%5==0)
{
rem=n-42;
  
if (rem > 42)
{
res=bears(rem);
}
else if(rem==42)
{
res=1;
}
else
{
res=0;
}
  
}

else if ((n%3==0) || (n%4==0))

{
rem=n-((n%10)*((n%100)/10));
if ((rem >42) && (((rem%10)*((rem%100)/10))==0))
{
if (rem%2==0)
{
rem=rem-rem/2;
res=bears(rem);
}
}
else if(rem>42 && (((rem%10)*((rem%100)/10))!=0))
{
if((rem-((rem%10)*((rem%100)/10)))==42)
{
res=1;
}
else
{
if (rem%2==0)
{
rem=rem-rem/2;
res=bears(rem);
}
}
}
  
  
else if(rem==42)
{
res=1;
}
else if(rem<42)
{
res=0;
}

}
return res;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote