C program to convert Decimal to Binary

Last updated on 03rd July 2013

This is a simple C program to convert a decimal number(base 10) to binary(base 2) format.

The program divides the decimal number repeatedly by 2 while storing the remainder in an array.

The final binary value is obtained by printing the array in reverse order.

For example if the user inputs 12, the program converts it to 1100

Program Source Code

/* **********************************************
 * Program to convert Decimal to Binary		*
 ************************************************/

#include <stdio.h>

int main() 
{
 int num, bin_num[100], dec_num, i,j;

 // Read an integer number
 printf("Enter an integer number\n");
 scanf("%d",&num);
 dec_num = num;

 // Convert Decimal to Binary
 i=0;
 while (dec_num) {
	bin_num[i] = dec_num % 2;
	dec_num = dec_num / 2;
	i++;
 }
 
 // Print Binary Number
 printf("The binary value of %d is ",num);
 for (j=i-1; j>=0; j-- ) {
	 printf("%d",bin_num[j]);
 }

  return 0;
}

Program Output

Enter an integer number
200
The binary value of 200 is 11001000

This program uses the concept of binary representation of decimal numbers and the process of division by 2 to convert decimal to binary.


Post a comment

Comments

sulav | February 16, 2016 10:36 AM |

you can use following program if u r a beginner in c language...... this is the most possible simplest way to convert decimal number to binary #include #include void main() { int i=0,b=1,n; printf("enter a number"); scanf("%d",&n); while(n) //when n becomes zero loop inside "while" stops or you can use while(n!=0) or while(n>0) and so on...... { i=