AD1

Tuesday, 15 November 2016

Write a recursive function that finds the binary equivalent of an integer 

solution 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
void binary(int decimal)
{
if(decimal==0){printf("%d",0);}
if(decimal>0)
{
if(decimal%2==0)
{
printf("%d\n",0);
binary(decimal/=2);
}
else
{
printf("%d\n",1);
binary(decimal/2);
}
}
return;
}
int main()
{
int n;
printf("enter decimal value\n");
scanf("%d",&n);
printf("binary equivalent of %d = \n",n);
binary(n);
return 0;
}
view raw binary.c hosted with ❤ by GitHub

No comments:

Post a Comment