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

write a c program to do the following: a) To find the area of a triangle. b) To convert the temperature from Fahrenheit to Celsius. c) To convert the time in hours : minutes : seconds to seconds.

0 Answers  


What are the uses of a pointer?

0 Answers  


Explain the use of function toupper() with and example code?

0 Answers  


what is the code to display color fonts in the output?

1 Answers  


Write a program to find given number is even or odd without using any control statement.

2 Answers  






Explain how do you list files in a directory?

0 Answers  


What is the result main() { char c=-64; int i=-32 unsigned int u =-16; if(c>i){ printf("pass1,"); if(c<u) printf("pass2"); else printf("Fail2");} else printf("Fail1); if(i<u) printf("pass2"); else printf("Fail2") } a)Pass1,Pass2 b)Pass1,Fail2 c)Fail1,Pass2 d)Fail1,Fail2 e)none

9 Answers   IBM,


the constant value in the case label is followed by a a) semicolon b) colon c) braces d) none of the above

0 Answers  


please give me some tips for the selection in TCS.

3 Answers   TCS,


What is the use of clrscr?

0 Answers  


which operator having lowest precedence?? a.)+ b.)++ c.)= d.)%

4 Answers  


Differentiate between null and void pointers.

0 Answers   TCS,


Categories