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
how should functions be apportioned among source files?
Is main is a keyword in c?
What is const and volatile in c?
An organised method of depicting the use of an area of computer memory used to signify the uses for different parts of the memory a) swap b) extended memory c) memory map d) all of the above
How can I avoid the abort, retry, fail messages?
What is restrict keyword in c?
How do you print an address?
Why is c platform dependent?
Why functions are used in c?
What is the difference between if else and switchstatement
the 'sizeof' operator reported a larger size than the calculated size for a structure type. What could be the reason?
Which is more efficient, a switch statement or an if else chain?
What are the different types of control structures?
Is swift based on c?
Write a program to check armstrong number in c?