C program to convert binary into hexadecimal.

 

01 #include <stdio.h>
02
03 int main()
04 {
05 int hexadecimal[1000],dec = 0;
06
07 long int binary;
08
09 int i=1,j,rem;
10
11 printf("Enter a Binary Number : ");
12 scanf("%ld",&binary);
13
14 /*binary to decimal*/
15
16 while(binary>0)
17 {
18 rem = binary % 2;
19 dec = dec + (rem*i);
20 binary = binary / 10;
21 i=i*2;
22 }
23
24 /* now decimal to hexadecimal*/
25
26 i = 0;
27
28 while(dec!=0)
29 {
30 hexadecimal[i] = dec % 16;
31 dec = dec/16;
32 i++;
33 }
34
35 printf("HexaDecimal value : ");
36
37 for(j=i-1; j>=0; j--)
38 {
39 if(hexadecimal[j]>9)
40 printf("%c",hexadecimal[j]+55);
41 else
42 printf("%d",hexadecimal[j]);
43 }
44
45 return 0;
46 }
 
 
OUTPUT :

Enter a Binary Number : 101010
HexaDecimal value : 2A

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.