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 does malloc () calloc () realloc () free () do?
what is use of malloc and calloc?
How can I trap or ignore keyboard interrupts like control-c?
What is the purpose of the statement: strcat (S2, S1)?
write a c program to print the next of a particular no without using the arithmetic operator or looping statements?
How would you use the functions fseek(), freed(), fwrite() and ftell()?
Explain how do you sort filenames in a directory?
Given an array of 1s and 0s arrange the 1s together and 0s together in a single scan of the array. Optimize the boundary conditions?
What is the -> in c?
What is extern c used for?
What is size of union in c?
a program that performs some preliminary processing in C, it acts upon certain directives that will affect how the compiler does its work a) compiler b) loader c) directive d) preprocessor
What are the different properties of variable number of arguments?
What is the use of static variable in c?
What is the use of f in c?