C Program To Convert Decimal To Binary Number
Learn How To Convert Decimal To Binary in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code.
A Decimal Number consists of values from 0 to 9 and a Binary Number consists of only 0 and 1. To change a Decimal Integer into Binary value, a Modulus Operator in C has to be used.
Example
Binary Equivalent of 10 is 1010.
1 | gcc test.c -lm |
Method 1: C Program For Decimal To Binary Conversion using If – Else
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include<stdio.h> #include<stdlib.h> void conversion(int num, int base) { int remainder = num % base; if(num == 0) { return; } conversion(num / base, base); if(remainder < 10) { printf("%d", remainder); } } int main() { int num, choice; printf("\nEnter a Positive Decimal Number:\t"); scanf("%d", &num); printf("\nBinary Value:\t"); conversion(num, 2); printf("\n"); return 0; } |
Method 2: C Program To Convert Decimal To Binary Value without Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h> int main() { int dec_num, rem, a = 1, temp; long int bin_num = 0; printf("\nEnter A Decimal Integer:\t"); scanf("%d", &dec_num); temp = dec_num; while(dec_num > 0) { rem = dec_num%2; dec_num = dec_num/2; bin_num = bin_num + (a * rem); a = a * 10; } printf("\nBinary Equivalent of Decimal Integer %d: %ld", temp, bin_num); printf("\n"); return 0; } |
Method 3: Convert Decimal Number To Binary in C using Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include<stdio.h> int decimal_to_binary(int dec_num) { int rem, a = 1; long int bin_num = 0; while(dec_num > 0) { rem = dec_num%2; dec_num = dec_num/2; bin_num = bin_num + (a * rem); a = a * 10; } return bin_num;lang:default decode:true } int main() { int dec_num, temp; printf("\nEnter A Decimal Integer:\t"); scanf("%d", &dec_num); temp = dec_num; printf("\nBinary Equivalent of Decimal Integer %d: %d", temp, decimal_to_binary(dec_num)); printf("\n"); return 0; } |
Output

If you have any compilation errors or doubts in this C Program To Convert Decimal to Binary Number, let us know about in the Comment Section below. Find more about Decimal Number System on Britannica.
Thanks for so many methods for converting a Decimal number into Binary values.
Wow! Decimal To Binary Conversion using If Else is so simple. Thanks. I finally understood the logic of conversion.
The first code for Decimal To Binary Conversion using Recursion is too good. It is just short simple and sweet.