Binary to hexadecimal conversion in c

C program for hexadecimal to binary conversion

#include<stdio.h>
int main(){
   
    long int binaryNumber,hexadecimalNumber=0,j=1,remainder;
   
    printf("Enter any number any binary number: ");
    scanf("%ld",&binaryNumber);
   
    while(binaryNumber!=0){
    remainder=binaryNumber%10;
    hexadecimalNumber=hexadecimalNumber+remainder*j;
        j=j*2;
        binaryNumber=binaryNumber/10;
      }
   
    printf("Equivalent hexadecimal value: %lX",hexadecimalNumber);

    return 0;
}

Sample output:

Enter any number any binary number: 1101
Equivalent hexadecimal value: D

No comments:

Post a Comment