You are on page 1of 1

void main()

{
int dec,rem,i=1;
long int bin=0;
printf("Enter the decimal number : ");
scanf("%d",&dec);
while(dec>0)
{
rem=dec%2;
dec=dec/2;
bin=bin+(i*rem);
i=i*10;
}
printf("The binary number is %l",bin);
getch();
}

Explanation:
The output variable bin is taken as long int bcoz it
might
exceed the range of normal int.

e.g.
dec=25

Then

bin=(1*1)+(10*0)+(100*0)+(1000*1)+(10000*1)
=11001

You might also like