Write a C program to convert an integer into a binary
string?



Write a C program to convert an integer into a binary string?..

Answer / vadivelt

#include<stdio.h>
char *IntToBinString(int no);
main()
{
int no;
printf("ENTER THE NO: ");
scanf("%d",&no);
printf("\nBINARY O/P STRING:\n%s",IntToBinString(no));
getch();
}
char *IntToBinString(int no)
{
char *ptr;
int i, size;
size = sizeof(int)*8;
ptr = (char *)malloc(sizeof(int)*8);
for(i = size - 1; i >= 0; i--)
{
if(no >> i & 0x01)
{
*ptr++ = 49;
}
else
{
*ptr++ = 48;
}
}
*ptr = '\0';
return (ptr - size);
}

Is This Answer Correct ?    9 Yes 3 No

Post New Answer

More C Interview Questions

any string of bits of length 'n' represents a unique non- negative integer between.............?

2 Answers  


what is difference b/w extern & volatile variable??

6 Answers   Teleca,


Do you have any idea about the use of "auto" keyword?

0 Answers  


we compile c program in 32 processor and 64 bit processor .exe file is created in both the processors. if we want to run .exe file in 64 bit processor which is created in 32 bit processor. is that .exe file is run or not if it is not run why?

4 Answers   HP, Wipro,


What is keyword with example?

0 Answers  






write a 'c' program to sum the number of integer values

8 Answers  


What is the symbol indicated the c-preprocessor?

0 Answers  


What is calloc()?

0 Answers   Adobe,


How can you convert integers to binary or hexadecimal?

0 Answers  


Stimulate calculator using Switch-case-default statement for two numbers

0 Answers   Wipro,


What header files do I need in order to define the standard library functions I use?

0 Answers  


Which function in C can be used to append a string to another string?

0 Answers  


Categories