Responsive Ads Here

Tuesday, 2 May 2017

Convert integer to binary by using recursion


#include<iostream>
void printbinary(int n)
{
if(n==0 || n==1)
{
std::cout<<n;
return;
}
printbinary(n/2);  //this function keep calling itself till we arrive at the terminating condition
std::cout<<n%2;// it will print all the numbers in reverse order i mean after the end of terminating //condition
}
int main()
{
int n;
std::cin>>n;
printbinary(n);// We pass the integer to the funtion.
return 0;
}

No comments:

Post a Comment