Skip to content

Instantly share code, notes, and snippets.

@ABD-01
Last active April 21, 2020 11:27
Show Gist options
  • Save ABD-01/4b98b51b7048fdf7d744b194e420ec2a to your computer and use it in GitHub Desktop.
Save ABD-01/4b98b51b7048fdf7d744b194e420ec2a to your computer and use it in GitHub Desktop.
Read an integer from user and out it in binary format. Write a recursive function.
#include <stdio.h>
#include <math.h>
int binary(int );
int main()
{
int num;
printf("Enter a num: ");
scanf(" %i",&num);
printf("The output in Binary is: %i \n",binary(num));
return 0;
}
int binary(int x)
{
if(x==0)
return 0;
int dig=log(x) / log(2);
return (x%2)*pow(10,dig) + binary(x/2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment