C Program to Convert Binary to Hexadecimal

Problem:

Write a C program to convert binary number to hexadecimal number.

Solution:

  1. Accept a binary number as input.
  2. Divide the binary number into group of four bits, multiply each it with the power of 2 and then add each time.
  3. Sum up the result of all groups to get the hexadecimal number as output.

C Program / Source Code:

Here is the source code of C program to convert binary to hexadecimal
 /* Aim: C Program to Convert Binary to Hexadecimal */

#include<stdio.h>

void main()
{

	long int i=1,bin,hex=0,rem;

	printf("\n Enter a binary number:- ");
	scanf("%ld",&bin); // accepting the binary number

	while(bin!=0)
	{
	rem=bin%10;
	hex=hex+rem*i;
	i*=2;
	bin/=10;
	}

	printf("\n Equivalent hexadecimal number is %lX", hex);
}

/* Output of above code / Runtime test cases:-

 Enter a binary number:- 10101111                                                                                  
                                                                                                                   
 Equivalent hexadecimal number is AF

*/