codescracker


c

C Program to Convert Hexadecimal to Binary



« Previous Program Next Program »


Hexadecimal to Binary Conversion in C

To convert hexadecimal number to binary number in C programming, you have to ask to the user to enter the hexadecimal number to convert it into binary number to display the equivalent value in binary

C Programming Code to Convert Hexadecimal to Binary

Following C program ask to the user to enter any number in hexadecimal to convert it into binary, then display the result on the screen:

/* C Program - Hexadecimal to Binary Conversion */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	long int i=0;
	char binnum[100], hexdec[100];
	printf("Enter any hexadecimal number : ");
	scanf("%s",hexdec);
	printf("\nEquivalent Binary value is : ");
	while(hexdec[i])
	{
		switch(hexdec[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("\nInvalid hexadecimal digit %c",hexdec[i]);
		}
		i++;
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program convert hexadecimal to binary

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test