In: Computer Science
For C++
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
#include <iostream>
using namespace std;
int search(int arr[],int key,int size)
{
size=size-1;
if(size <0)
{
return -1;
}
else if(arr[size]==key)
{
return size;
}
else
{
return search(arr,key,size);
}
}
void printToBinary(int num)
{
if (num>0)
{
printToBinary(num/2);
cout<<num%2 << " ";
}
}
int main()
{
int arr[]={1,12,33,4,10,-3,6,100,11,45,-5,-9,0,245,90};
int size=15;
int num;
cout<<"Enter a number: ";
cin>> num;
int pos=search(arr,num,size);
if(pos==-1)
cout<<"The number is not found in the
array."<<endl;
else
{
cout<<"The number is found at
"<<pos<<" position."<<endl;
cout<<"The Binary representation of number is:
";
printToBinary(num);
}
return 0;
}
============================