Decimal to Binary Conversion.

Decimal to binary conversion

  C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).
#include<stdio.h>

int main(){

    long int decimalNumber,remainder,quotient;

    int binaryNumber[100],i=1,j;

    printf("Enter any decimal number: ");

    scanf("%ld",&decimalNumber);

    quotient = decimalNumber;

    while(quotient!=0){

         binaryNumber[i++]= quotient % 2;

         quotient = quotient / 2;

    }

    printf("Equivalent binary value of decimal number %d: ",decimalNumber);

    for(j = i -1 ;j> 0;j--)

         printf("%d",binaryNumber[j]);

    return 0;

}

Sample output:
Enter any decimal number: 50
Equivalent binary value of decimal number 50: 110010

No comments:

Post a Comment