Input
Enter a Binary number : 11101
Output
Hexadecimal equivalent of given number : 1D
Input
Enter a Binary number : 1110
Output
Hexadecimal equivalent of given number : E
Step 1: START
Step 2: Enter a binary number 'num' as an input.
Step 3: Firstly we have to convert binary number into deciaml form.
Step 4: After that convert into Hexadecimal form
Step 4: END
/* Binary to hexadecimal Conversion */
#include <stdio.h>
#include <math.h>
int main()
{
int bin,decimal=0,rem,i,j;
char hexa[10];
printf(" Enter the binary number ");
scanf("%d",&bin);
for(i=0;bin>0;i++)
{
rem=bin%10;
decimal=decimal+rem*pow(2,i);
bin/=10;
}
/* binary --> deciaml --> octal */
for(i=0;decimal>0;i++)
{
rem=decimal%16;
if(rem<10)
hexa[i]=rem+48;
else
hexa[i]=rem+55;
decimal/=16;
}
printf(" Hexadecimal of given number is = ");
for(j=i-1;j>=0;j--)
printf("%c",hexa[j]);
return 0;
}
/* Binary to hexadecimal Conversion */
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int bin,decimal=0,rem,i,j;
char hexa[10];
cout<<" Enter the binary number : ";
cin>>bin;
for(i=0;bin>0;i++)
{
rem=bin%10;
decimal=decimal+rem*pow(2,i);
bin/=10;
}
/* binary --> deciaml --> octal */
for(i=0;decimal>0;i++)
{
rem=decimal%16;
if(rem<10)
hexa[i]=rem+48;
else
hexa[i]=rem+55;
decimal/=16;
}
cout<<" Hexadecimal of given number is = ";
for(j=i-1;j>=0;j--)
cout<<hexa[j];
return 0;
}
Enter a Binary number : 11101
Hexadecimal equivalent of given number : 1D
advertisment