# C Program to Convert Binary to Hexadecimal

«
»

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.

1. `/*`
2. ` * C Program to Convert Binary to Hexadecimal `
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int main()`
7. `{`
8. `    long int binaryval, hexadecimalval = 0, i = 1, remainder;`
9. ` `
10. `    printf("Enter the binary number: ");`
11. `    scanf("%ld", &binaryval);`
12. `    while (binaryval != 0)`
13. `    {`
14. `        remainder = binaryval % 10;`
15. `        hexadecimalval = hexadecimalval + remainder * i;`
16. `        i = i * 2;`
17. `        binaryval = binaryval / 10;`
18. `    }`
19. `    printf("Equivalent hexadecimal value: %lX", hexadecimalval);`
20. `    return 0;`
21. `}`
Program Explanation

1. Take a binary number as input and store it in the variable binaryval.
2. Obtain the remainder and quotient of the input number by dividing it by 10.
3. Multiply the obtained remainder with variable i and increment the variable hexadecimalval with this value.
4. Increment the variable i by 2 and override the variable binaryval with the quotient obtained.
5. Repeat the steps 2-4 until the variable binaryval becomes zero.
6. Print the variable hexadecimalval as output.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
```Output:
Enter the binary number: 10000

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples. 