Here is a C++ program to convert decimal to hexadecimal. Here are the different methods to convert decimal to hexadecimal like using recursion, using manipulator etc.
How to convert decimal to hexadecimal (base 10 to base 16).
To convert decimal to hexadecimal a simple method is to continuously divide the number by 16 until final result is zero, in each division we get remainder, simply wrote the remainders starting from bottom to upwards .Here it should be noted that if the remainder is greater then 9 than we use alphabets A for 10, B for 11, C for 12, D for 13, E for 14 and F for 15.
For example let us convert decimal number 26 to hexadecimal.
26 / 16 quotient = 1 ,rem = 10 or A
1 / 16 quotient = 0 ,rem = 1
Thus hexadecimal equivalent of decimal number (16)10 is 1A16.
Method 1 : C++ program to convert decimal to hexadecimal
Method 2 : C++ program to convert decimal to hexadecimal using functions
Method 3 : C++ program to convert decimal to hexadecimal using recursion
Method 4 : C++ program to convert decimal number to hexadecimal using Manipulators
Method 5 : C++ program to convert decimal to hexadecimal and store it as string
How to convert decimal to hexadecimal (base 10 to base 16).
To convert decimal to hexadecimal a simple method is to continuously divide the number by 16 until final result is zero, in each division we get remainder, simply wrote the remainders starting from bottom to upwards .Here it should be noted that if the remainder is greater then 9 than we use alphabets A for 10, B for 11, C for 12, D for 13, E for 14 and F for 15.
For example let us convert decimal number 26 to hexadecimal.
26 / 16 quotient = 1 ,rem = 10 or A
1 / 16 quotient = 0 ,rem = 1
Thus hexadecimal equivalent of decimal number (16)10 is 1A16.
Method 1 : C++ program to convert decimal to hexadecimal
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
long i,hexa[20],num,temp;
cout<<"Enter the number ";
cin>>num;
for(i=0;num>0;i++)
{
hexa[i]=num%16;
num/=16;
}
cout<<"Hexadecimal equivalent : ";
while(i>0)
{
i--;
if(hexa[i]>9)
cout<<char(55+hexa[i]);
else
cout<<hexa[i];
}
return 0;
}
#include<stdio.h>
using namespace std;
int main()
{
long i,hexa[20],num,temp;
cout<<"Enter the number ";
cin>>num;
for(i=0;num>0;i++)
{
hexa[i]=num%16;
num/=16;
}
cout<<"Hexadecimal equivalent : ";
while(i>0)
{
i--;
if(hexa[i]>9)
cout<<char(55+hexa[i]);
else
cout<<hexa[i];
}
return 0;
}
Method 2 : C++ program to convert decimal to hexadecimal using functions
#include<iostream>
using namespace std;
void hex(int);
int main()
{
int n;
cout<<"Enter number : ";
cin>>n;
cout<<"Hexadecimal equivalent : ";
hex(n);
return 0;
}
void hex(int num)
{
int hexa[20],i;
for(i=0;num>0;i++)
{
hexa[i]=num%16;
num/=16;
}
while(i>0)
{
i--;
if(hexa[i]>9)
cout<<char(55+hexa[i]);
else
cout<<hexa[i];
}
}
using namespace std;
void hex(int);
int main()
{
int n;
cout<<"Enter number : ";
cin>>n;
cout<<"Hexadecimal equivalent : ";
hex(n);
return 0;
}
void hex(int num)
{
int hexa[20],i;
for(i=0;num>0;i++)
{
hexa[i]=num%16;
num/=16;
}
while(i>0)
{
i--;
if(hexa[i]>9)
cout<<char(55+hexa[i]);
else
cout<<hexa[i];
}
}
Method 3 : C++ program to convert decimal to hexadecimal using recursion
#include<iostream>
using namespace std;
void hex(int n)
{
if(n==0)
return ;
hex(n/16);
if(n%16>9)
cout<<char(n%16+55);
else
cout<<n%16;
}
int main()
{
int num;
cout<<"Enter Decimal number : ";
cin>>num;
cout<<"Hexadecimal equivalent : ";
hex(num);
return 0;
}
using namespace std;
void hex(int n)
{
if(n==0)
return ;
hex(n/16);
if(n%16>9)
cout<<char(n%16+55);
else
cout<<n%16;
}
int main()
{
int num;
cout<<"Enter Decimal number : ";
cin>>num;
cout<<"Hexadecimal equivalent : ";
hex(num);
return 0;
}
Method 4 : C++ program to convert decimal number to hexadecimal using Manipulators
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter number : ";
cin>>n;
cout<<"Hexadecimal equivalent : "<<hex<<n;
return 0;
}
using namespace std;
int main()
{
int n;
cout<<"Enter number : ";
cin>>n;
cout<<"Hexadecimal equivalent : "<<hex<<n;
return 0;
}
Method 5 : C++ program to convert decimal to hexadecimal and store it as string
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[20],temp;
int i,n;
cout<<"Decimal equivalent : ";
cin>>n;
for(i=0;n>0;i++)
{
temp=n%16;
if(n>9)
a[i]=temp+55;
else
a[i]=temp+48;
n=n/16;
}
a[i]='\0';
strrev(a);
cout<<"Hexadecimal equivalent : "<<a;
}
#include<string.h>
using namespace std;
int main()
{
char a[20],temp;
int i,n;
cout<<"Decimal equivalent : ";
cin>>n;
for(i=0;n>0;i++)
{
temp=n%16;
if(n>9)
a[i]=temp+55;
else
a[i]=temp+48;
n=n/16;
}
a[i]='\0';
strrev(a);
cout<<"Hexadecimal equivalent : "<<a;
}
No comments:
Post a Comment