C program to convert decimal to binary without Array

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ 
 int n, q, rem, i, bin, rev, flag=0,k=0;
 clrscr();
 printf("Enter the decimal number: ");
 scanf("%d",&n);
 q=n;
 rev=0;
while(q>0)    
{
 rem=q%2;
if(rem==0 && flag==0)
 k++;
 else
 flag=1;
 rev=(rev*10)+rem;
 q=q/2;
}
 q=rev;
 bin=0;
while(q>0)    
{
 rem=q%10;
 bin=(bin*10)+rem;
 q=q/10;
if(q==0)
{
for(i=1;i<=k;i++)
 bin=bin*10;
}
}
 printf("The binary number is %d", bin);
 getch();
}

Output:

C program to convert decimal to binary without Array

Leave a Reply

Your email address will not be published. Required fields are marked *