C Program to convert decimal number to binary number

Simple C program to convert decimal number to binary number.
#include<stdio.h>
#include<math.h>
int main()
{
 int n,binary=0,i=0;
 printf("Enter a number: ");
 scanf("%d",&n);
 while(n!=0)
 {
  binary=binary+pow(10,i)*(n%2);
  n/=2; //or n=n/2
  i++;
 }
 printf("Binary number = %d",binary);
 return 0;
}
C program To convert decimal to binary using array
#include<stdio.h>
#include<math.h>
int main()
{
 int n,i=0,j;
 int arr[100];
 printf("Enter a number: ");
 scanf("%d",&n);
 while(n!=0)
 {
  arr[i]=n%2;
  n=n/2;
  i++;
 }
 printf("Binary number = ");
 for(j=i-1;j>=0;j--)
 {
  printf("%d",arr[j]);
 }
 
 return 0;
}



Popular posts from this blog