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