codescracker


c++

C++ Program to Convert Binary to Hexadecimal



« Previous Program Next Program »


Binary to Hexadecimal in C++

To convert binary to hexadecimal in C++ Programming, you have to ask to the user to enter any number in binary to convert it into hexadecimal, then display the equivalent hexadecimal value on the screen as shown here in the following program.

C++ Programming Code to Convert Binary to Hexadecimal

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

/* C++ Program - Binary to Hexadecimal */
		
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	long int binnum, rem, quot;
	int i=1, j, temp;
	char hexdecnum[100];
	cout<<"Enter Binary Number : ";
	cin>>binnum;
	quot = binnum;
	while(quot!=0)
	{
		temp = quot % 16;
		// To convert integer into character
		if( temp < 10)
		{
			temp = temp + 48;
		}
		else
		{
			temp = temp + 55;
		}
		hexdecnum[i++]= temp;
		quot = quot / 16;
	}
	cout<<"Equivalent hexadecimal value of "<<binnum<<" is :\n";
	for(j=i-1 ;j>0;j--)
	{
		cout<<hexdecnum[j];
	}
	getch();
}

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

C++ program to convert binary to hexadecimal

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test