C program to convert decimal to binary

This is a simple C program to convert a decimal number to binary.

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>
#include <conio.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]);
 }

  
 // Wait for key press
 getch();

return 0;
}

Program Output

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


Post a comment

Name

Your Comment

Email (We dont publish it)

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=