
Patrick B. answered 02/02/21
Math and computer tutor/teacher
It really doesn't.. It is not very good. If the incorrect value is passed it will not work at all.
The recursion is not the best design either.
Here's my response and it works
#include <stdio.h>
#include <string.h>
#include <math.h>
DecimalToBinary( long longIntNum, char* bitStr )
{
int iLoop=0;
memset(bitStr,'0',32);
long base2;
for (iLoop=0; iLoop<32 ; iLoop++)
{
base2 = pow(2,(31-iLoop));
bitStr[iLoop]= (longIntNum/base2) ? '1' : '0';
longIntNum%=base2;
}
}
int main()
{
char bitStr[33]; bitStr[32]=0;
long longIntNum=56789;
DecimalToBinary(longIntNum,bitStr);
printf("%ld decimal in binary is %s \n",longIntNum,bitStr);
//1101 1101 1101 0101
}