This is a C program to Convert Binary to Hexadecimal.
Problem Description
This program takes a binary number as input and converts to hexadecimal.
Problem Solution
1. Take a binary number as input.
2. Divide the binary number into groups of 4 bits. For each group of 4 bits, multiply each bit with the power of 2 and add them consecutively.
3. Combine the result of all groups to get the output.
Program/Source Code
Here is source code of the C program to Convert Binary to Hexadecimal . The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Convert Binary to Hexadecimal
*/
#include
int main()
{
long int binaryval, hexadecimalval = 0, i = 1, remainder;
printf(“Enter the binary number: “);
scanf(“%ld”, &binaryval);
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
printf(“Equivalent hexadecimal value: %lX”, hexadecimalval);
return 0;
}
Runtime Test Cases
Output:
Enter the binary number: 10000
Equivalent hexadecimal value: 10