Written by Sai Sameera Thota on January 20, 2013 in C > Programming

C Program to Convert Decimal to Binary Number

GET ALERTS:

Get our Latest updates delivered to your mailbox!

This article helps you in Convertion of Decimal Number to Binary Number using C Programming. We are declaring 5 variables. First of all we are reading the input into the varible dec and we are copying it into the variable num. Then we are storing the remainder obtained from num%2 in the bin[] array by increment of i. Then we are printing the binary equivalent by printing the elements of array in reverse order using for loop.

Example:
If the given numbr is 25
num remainder
25/2 = 12 1
12/2 = 6 0
6/2 = 3 0
3/2 = 1 1
1/2 = 0 1

#include<conio.h>
#include<stdio.h>
void main()
{
      int num, bin[50], dec, i=1, j;
      printf("Enter the decimal number : ");
      scanf("%d",&dec);
      
      num = dec;
      while(num!=0)
      {
                   bin[i++] = num%2;
                   num = num/2;
      }
      printf("\nBinary equivalent of %d : ",dec);
      for(j=i-1;j>0;j--)
      {
                   printf("%d",bin[j]);
      }
      getch();
}                    

Output:

decimal-to-binary-conversion

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: