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
main() { printf("hello"); fork(); }
Is it better to use a macro or a function?
If null and 0 are equivalent as null pointer constants, which should I use?
Write a program to identify if a given binary tree is balanced or not.
What is the purpose of void in c?
What are lookup tables in c?
Explain goto?
Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;
What are predefined functions in c?
Why doesnt long int work?
Do you know the purpose of 'register' keyword?
What is structure in c explain with example?
What is a list in c?
When should you not use a type cast?
What is the use of c language in real life?