Write a C program to convert an integer into a binary
string?
Answer Posted / 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 View All Answers
What are header files why are they important?
Explain zero based addressing.
What are the rules for identifiers in c?
What is hungarian notation? Is it worthwhile?
What are the two types of functions in c?
Tell me can the size of an array be declared at runtime?
Why can’t we compare structures?
How can I get the current date or time of day in a c program?
Difference between Shallow copy and Deep copy?
What is "Hungarian Notation"?
What is atoi and atof in c?
What standard functions are available to manipulate strings?
Who is the founder of c language?
Which one to choose from 'initialization lists' or 'assignment', for the use in the constructor?
Write a program in c to replace any vowel in a string with z?