C Program for the Conversion of Decimal to Binary Number


Description:
Decimal Numbers:
Decimal numbers has a radix 10. Decimal numbers use a combination of 0,1,2,3,4,5,6,7,8,9.
Binary Numbers:
Binary numbers has a radix of 2. Binary numbers use a combination of 0’s and 1’s.
Conversion from decimal number system to binary number system can be done by factorize the decimal number by 2

This code will work for decimal numbers up to 1023 and only for integers, due to the data type restriction of C language.

Source Code:
/* 
            Decimal to Binary Conversion using C 
            Author: codershandbook.blogspot.com 
*/  
int main(void)  
{   unsigned long dec_no,bin_no=0,iter=1;  
 printf("\n\t\tDecimal to Binary Conversion");  
 printf("\n\tEnter the Decimal number:");  
 scanf("%ld",&dec_no);  
 while(dec_no>1)  
 {  
            bin_no=bin_no+(dec_no%2)*iter;  
            iter*=10;  
            dec_no/=2;  
 }  
 bin_no=bin_no+dec_no*iter;  
 printf("\nBinary equivalent is :%ld",bin_no);  
Previous
Next Post »

Still not found what you are looking for? Try again here.