How to Convert Number Decimal to Binary in C

A Simple C Program to Convert the Decimal into Binary Numbers. We have used a While loop, Modulus(%) and Division(/) operator for this task. We have used an Array "ar" to store the Binary Output(remainder) for proper formatting of output.

Source Code

#include<stdio.h>
int main()
{
 int a=0,b=0,n,i=1,j,k=0;
 int ar[10]={0,0,0,0,0,0,0,0,0,0};
 printf("\n \n ***************** CONVERT DECIMAL TO BINARY ****************");
 printf("\n \n Enter Decimal Number : ");
 scanf("%d",&n);
 while(n!=0)
 {
  a=n%2;
  b=n/2;
  ar[i++]=a;
  n=b;
  k++;
 }
 
 printf(" \n \n BINARY NUMBER : ");

 for(j=k+1;j>=1;j--)
 {
  printf("%d",ar[j]);
 }
 
 printf("\n \n ");

return 0;
}

Output of the Program


Conversion between Decimal to Binary

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment