C Program to Convert Hexadecimal to Binary

Problem:

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

Algorithm or Solution:

Here is the algorithm or logical solution of C program to convert hexadecimal number to octal number.
  1. Start
  2. Accept hexadecimal number as input.
  3. Use switch case to print binary number for each bit of the hexadecimal input.
  4. Ultimately binary equivalent of hexadecimal input is printed.
  5. Stop.

C Program / Source Code:

Here is the source code of c program to convert hexadecimal to binary.
/* Aim: C program to convert Hexadecimal number to its equivalent Binary */

#include<stdio.h>
#define SIZE 100

int main()
{
	char b_num[SIZE], hex_no[SIZE];
	int i=0;

	printf("\n Enter hexadecimal number:- ");
	scanf("%s",hex);

	printf("\n Equivalent binary number is ");

	while(hex_no[i])
	{

		switch(hex_no[i])
		{
		case '0': 
			printf("0000"); break;

		case '1':
			printf("0001"); break;

		case '2':
			printf("0010"); break;

		case '3':
			printf("0011"); break;

		case '4':
			printf("0100"); break;

		case '5':
			printf("0101"); break;

		case '6':
			printf("0110"); break;

		case '7':
			printf("0111"); break;

		case '8':
			printf("1000"); break;

		case '9':
			printf("1001"); break;

		case 'A':
			printf("1010"); break;

		case 'B':
			printf("1011"); break;

		case 'C':
			printf("1100"); break;

		case 'D':
			printf("1101"); break;

		case 'E':
			printf("1110"); break;

		case 'F':
			printf("1111"); break;

		case 'a':
			printf("1010"); break;

		case 'b':
			printf("1011"); break;

		case 'c':
			printf("1100"); break;

		case 'd':
			printf("1101"); break;

		case 'e':
			printf("1110"); break;

		case 'f':
			printf("1111"); break;

		default:
			printf("\n Invalid hexadecimal digit %c \n \n", hex[i]); /* As there are only 1-9 digits and a-f or A-F characters allowed in hexadecimal number */
		}

		i++;

	}

	return 0;
}


/* Output of above code:-

Enter hexadecimal value:- ab
Equivalent binary number is 10101011

*/
Get This Program: