Program for decimal to hexadecimal conversion
Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent hexadecimal number. i.e. convert the number with base value 10 to base value 16.
Hexadecimal numbers use 16 values to represent a number. Numbers from 0-9 are expressed by digits 0-9 and 10-15 are represented by characters from A – F.
Examples:Â Â
Input : 116
Output: 74
Input : 10
Output: A
Input : 33
Output: 21
Algorithm:
- Store the remainder when the number is divided by 16 in a temporary variable temp. If the temp is less than 10, insert (48 + temp) in a character array otherwise if the temp is greater than or equal to 10, insert (55 + temp) in the character array.
- Divide the number by 16 now
- Repeat the above two steps until the number is not equal to 0.
- Print the array in reverse order now.
Example
If the given decimal number is 2545.Â
Step 1: Calculate the remainder when 2545 is divided by 16 is 1. Therefore, temp = 1. As temp is less than 10. So, arr[0] = 48 + 1 = 49 = ‘1’.Â
Step 2: Divide 2545 by 16. The new number is 2545/16 = 159.Â
Step 3: Calculate the remainder when 159 is divided by 16 is 15. Therefore, temp = 15. As temp is greater than 10. So, arr[1] = 55 + 15 = 70 = ‘F’.Â
Step 4: Divide 159 by 16. The new number is 159/16 = 9.Â
Step 5: Calculate the remainder when 9 is divided by 16 is 9. Therefore, temp = 9. As temp is less than 10. So, arr[2] = 48 + 9 = 57 = ‘9’.Â
Step 6: Divide 9 by 16. The new number is 9/16 = 0.Â
Step 7: Since the number becomes = 0. Stop repeating steps and print the array in reverse order. Therefore, the equivalent hexadecimal number is 9F1.
The below diagram shows an example of converting the decimal number 2545 to an equivalent hexadecimal number. Â
Below is the implementation of the above idea.Â
C++
#include <iostream>
using namespace std;
string decToHexa( int n)
{
string ans = "" ;
while (n != 0) {
int rem = 0;
char ch;
rem = n % 16;
if (rem < 10) {
ch = rem + 48;
}
else {
ch = rem + 55;
}
ans += ch;
n = n / 16;
}
int i = 0, j = ans.size() - 1;
while (i <= j)
{
swap(ans[i], ans[j]);
i++;
j--;
}
return ans;
}
int main()
{
int n = 2545;
cout << decToHexa(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void decToHexa( int n)
{
char [] hexaDeciNum = new char [ 100 ];
int i = 0 ;
while (n != 0 ) {
int temp = 0 ;
temp = n % 16 ;
if (temp < 10 ) {
hexaDeciNum[i] = ( char )(temp + 48 );
i++;
}
else {
hexaDeciNum[i] = ( char )(temp + 55 );
i++;
}
n = n / 16 ;
}
for ( int j = i - 1 ; j >= 0 ; j--)
System.out.print(hexaDeciNum[j]);
}
public static void main(String[] args)
{
int n = 2545 ;
decToHexa(n);
}
}
|
Python3
def decToHexa(n):
hexaDeciNum = [ '0' ] * 100
i = 0
while (n ! = 0 ):
temp = 0
temp = n % 16
if (temp < 10 ):
hexaDeciNum[i] = chr (temp + 48 )
i = i + 1
else :
hexaDeciNum[i] = chr (temp + 55 )
i = i + 1
n = int (n / 16 )
j = i - 1
while (j > = 0 ):
print ((hexaDeciNum[j]), end = "")
j = j - 1
n = 2545
decToHexa(n)
|
C#
using System;
class GFG {
static void decToHexa( int n)
{
char [] hexaDeciNum = new char [100];
int i = 0;
while (n != 0) {
int temp = 0;
temp = n % 16;
if (temp < 10) {
hexaDeciNum[i] = ( char )(temp + 48);
i++;
}
else {
hexaDeciNum[i] = ( char )(temp + 55);
i++;
}
n = n / 16;
}
for ( int j = i - 1; j >= 0; j--)
Console.Write(hexaDeciNum[j]);
}
public static void Main(String[] args)
{
int n = 2545;
decToHexa(n);
}
}
|
Javascript
<script>
function decToHexa(n)
{
var hexaDeciNum = Array.from({length: 100},
(_, i) => 0);
var i = 0;
while (n != 0) {
var temp = 0;
temp = n % 16;
if (temp < 10) {
hexaDeciNum[i] =
String.fromCharCode(temp + 48);
i++;
}
else {
hexaDeciNum[i] =
String.fromCharCode(temp + 55);
i++;
}
n = parseInt(n / 16);
}
for (j = i - 1; j >= 0; j--)
document.write(hexaDeciNum[j]);
}
var n = 2545;
decToHexa(n);
</script>
|
PHP
<?php
function decToHexa( $n )
{
$hexaDeciNum ;
$i = 0;
while ( $n != 0)
{
$temp = 0;
$temp = $n % 16;
if ( $temp < 10)
{
$hexaDeciNum [ $i ] = chr ( $temp + 48);
$i ++;
}
else
{
$hexaDeciNum [ $i ] = chr ( $temp + 55);
$i ++;
}
$n = (int)( $n / 16);
}
for ( $j = $i - 1; $j >= 0; $j --)
echo $hexaDeciNum [ $j ];
}
$n = 2545;
decToHexa( $n );
?>
|
Time complexity: O(log16n)
Auxiliary space: O(1)
Using Predefined function
C++
#include <iostream>
using namespace std;
void decToHexa( int n) { cout << hex << n << endl; }
int main()
{
int n = 2545;
decToHexa(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void decToHexa( int n)
{
System.out.println(Integer.toHexString(n));
}
public static void main(String[] args)
{
int n = 2545 ;
decToHexa(n);
}
}
|
Python3
def decToHexa(n):
return hex (n).replace( "0x" , "")
n = 2545
print (decToHexa(n))
|
C#
using System;
class GFG
{
public static void decToHexa( int n)
{
Console.Write(Convert.ToString(n, 16));
}
public static void Main(String[] args)
{
int n = 2545;
decToHexa(n);
}
}
|
Javascript
<script>
function decToHexa(n)
{
document.write(n.toString(16));
}
var n = 2545;
decToHexa(n);
</script>
|
Time Complexity: O(log16(n)), because we divide the n by 16 till it becomes zero.
Auxiliary Space: O(1), we cannot use any extra space.
Â
Last Updated :
22 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...