Decimal to Binary conversion

Here is C program to convert a decimal number into binary number. This program is asked frequently in interview. Many software companies ask this question.

C Program:

#include<stdio.h>
#include<math.h>
int main()
{
 int n;
 printf("Enter a decimal number: ");
 scanf("%d",&n);
 
 /* Here array is to store binary element */
 int arr[32],i=0,j;
 
 while(n!=0)
 {
  arr[i] = n%2;
  i++;
  n = n/2; 
 }
 
 for(j=i-1;j>=0;j--)
 {
  printf("%d",arr[j]);
 }
 
 return 0;
}

Popular posts from this blog