C program to convert Binary to Hexadecimal number

In this article, we are going to learn the C program to Convert Binary to Hexadecimal numbers. Here we are going to learn about two number systems one is binary and another is Hexadecimal. Binary numbers are base 2 numbers and represented in 0s and 1s. Hexadecimal numbers are base 16 numbers and represented in numbers from 0 to 9 and A to F.

The binary to Hexadecimal equivalent number table is given below:

Decimal NumberBinary NumbersHexadecimal Number
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F

C program to convert binary to hexadecimal number


#include <stdio.h>
 
int main()
{
    long long binNum,tempBin;
    long int hexNum;

    int i = 1;
    int remainder;
 
    hexNum  = 0;
    printf("Please enter 8 bit binary number: ");
    scanf("%lld", &binNum);
    
    tempBin = binNum;

    while (binNum != 0)
    {
        remainder = binNum % 10;
        hexNum = hexNum + remainder * i;
        i = i * 2;
        binNum = binNum / 10;
    }
    
    printf("HexaDecimal Equivalent to Binary number = %lld is = %lX \n", tempBin,hexNum);
    
    return 0;
}