0% found this document useful (0 votes)
56 views4 pages

Using Namespace Bool Int Int If Return True If Return False If Return True If Return False Int If

The document contains C++ code that defines a bear function that takes an integer parameter n and returns a boolean value. The function uses conditionals and recursive calls to bear to determine if certain conditions on n are met, like if n is even it returns n/2 bears, or if the last two digits of n can be multiplied it returns that result. The code also includes a main function that gets user input for n, calls bear, and prints the return value.

Uploaded by

Chai Jin Ying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views4 pages

Using Namespace Bool Int Int If Return True If Return False If Return True If Return False Int If

The document contains C++ code that defines a bear function that takes an integer parameter n and returns a boolean value. The function uses conditionals and recursive calls to bear to determine if certain conditions on n are met, like if n is even it returns n/2 bears, or if the last two digits of n can be multiplied it returns that result. The code also includes a main function that gets user input for n, calls bear, and prints the return value.

Uploaded by

Chai Jin Ying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream>

using namespace std;


bool bear(int n)
{
int result,b,c;
result=2;
if(n==42)
return true;
if(n!=42)
return false;
if(result==1)
return true;
if(result==0)
return false;
int a;
a=n%5;
if((a==0)&&(result==2)){
result=bear(n-42);
a=n%4; // If n is divisible by 3 or 4, then you may multiply
the last two digits of n and //give back this many bears
}
if((a==0)&&(result==2))
{
b=n%10;
c=((n%100)-b)/10;
if((b!=0)&& (c!=0))
result=bear(n-c*b);
}
a=n%3; // If n is divisible by 3 or 4, then you may multiply
the last two digits of n and // give back this many bears.
if((a==0)&&(result==2))
{
b=n%10;
c=((n%100)-b)/10;
if((b!=0)&&(c!=0))
result=bear(n-c*b);
}
a=n%2; // If n is even, then you may give back exactly n/2
bears
if((a==0)&&(result==2))
result=bear(n/2);
else if(result==2)
false;

if(result==1)
true;
if(result==0)
false;

return n;
}
int main() // The game starts when I give you some bears
{
int n;
bool result;
cout<< "please enter count of bears : ";
cin>>n;
result=bear(n);
if(result==true)
cout<< "true";
else
cout<<"false";
}

#include <iostream>
bool bear(int n)
{
int result,b,c;
result=2;
if(n==42) return true;
if(n!=42) return false;
if(result==1)return true;
if(result==0)return false;
int a;
a=n%5;
if(a==0&amp;&amp;result==2)result=bear(n-42);
a=n%4; // If n is divisible by 3 or 4, then you may multiply
the last two digits of n and //give back this many bears
if(a==0&amp;&amp;result==2)
{
b=n%10;
c=((n%100)-b)/10;
if(b!=0&amp;&amp;c!=0) result=bear(n-c*b);
}
a=n%3; // If n is divisible by 3 or 4, then you may multiply
the last two digits of n and // give back this many bears.
if(a==0&amp;&amp;result==2)
{
b=n%10;
c=((n%100)-b)/10;
if(b!=0&amp;&amp;c!=0)result=bear(n-c*b);
}
a=n%2; // If n is even, then you may give back exactly n/2
bears
if(a==0&amp;&amp;result==2) result=bear(n/2);
else if(result==2)return false;
if(result==1)return true;
if(result==0)return false;
}
int main() // The game starts when I give you some bears
{
int n;
bool result;
printf(&quot;please enter count of bears : &quot;);
scanf(&quot;%d&quot;,&amp;n);
result=bear(n);
if(result==true) printf(&quot; true&quot;);
else printf(&quot; false&quot;);

scanf(&quot;%d&quot;,&amp;n);
}

You might also like