Very easy implementation for convertion a integer value to a binary values. Let us see the program and output. How it performs, i used a string and append output after getting remainder (giving condition on that). Eventually ended up with reverse string which is actual binary value of given number.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void convertToBytes(int a)
{
int i=0;
char b[10];
while(a>0)
{
if(a%2==0)
{
b[i]='0';
i++;
}
else
{
b[i]='1';
i++;
}
a=a/2;
}
b[i]='\0';
strrev(b);
printf("%s\n",b);
}
void main()
{
int n;
printf("Enter Your Number\n");
scanf("%d",&n);
convertToBytes(n);
getch();
}
// Output :
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment