Mega Code Archive

 
Categories / C / Beginners
 

Program to display hexadecimal equivalent of a input binary number

#include <stdio.h> #include <conio.h> #include <math.h> int btod(int b[]); void dtoh(int s); int c; void main() { int a[5],d,i,e,f,g,s; clrscr(); printf(" What will be the length of input no.? "); scanf("%d",&c); printf("Don't exceed input from your input limit or 5 digits in any case"); printf(" Enter no.:- "); scanf("%d",&d); for (i=c-1;i>=0;--i) { e = pow(10,i); a[i] = d/e; d = d%e; } s=btod(a); dtoh(s); printf(" HAVE A NICE DAY! BYE."); getch(); } int btod(int b[]) { int k,s=0,i,n; n=*b; for (i=c-1;i>=0;--i) { if (i>0) *b=*(b+i); else *b=n; if (*b==1) { k=pow(2,i); s=s+k; } } return(s); } void dtoh(int s) { int b,c=0,a[5],i=0; b=s; while (b>15) { a[i]=b%16; b=b/16; i++; c++; } a[i]=b; printf(" Its hexadecimal equivalent is "); for (i=c;i>=0;--i) { if (a[i]==10) printf("A"); else if (a[i]==11) printf("B"); else if (a[i]==12) printf("C"); else if (a[i]==13) printf("D"); else if (a[i]==14) printf("E"); else if (a[i]==15) printf("F"); else printf("%d",a[i]); } return; }