# Convert a decimal number to hexadecimal using recursion

Problem statement:- Program to convert a decimal number to hexadecimal using recursion.

Data requirement:-

Input Data:- n

Output Data:-
hexa, str1, str

Program in C

### Here is the source code of the C program to convert a decimal number to hexadecimal using recursion.

Code:

#include<stdio.h>
#include<string.h>
char *DecimalToHexadecimal(int n)
{
static char Hex[100];
static int rem,i=0;
if(n!=0)
{
rem=n%16;
//Convert Integer to char
if(rem<10)
Hex[i++]=rem+48;// 48 Ascii=0
else
Hex[i++]=rem+55;//55 Ascii=7
DecimalToHexadecimal(n/16);
}
return Hex;
}
int main()
{
int n;
printf("Enter the Decimal Value:");
scanf("%d",&n);
char *hexa;
hexa=DecimalToHexadecimal(n);
printf("Hexadecimal Value of Decimal number is: %s",strrev(hexa));
}

Input/Output:
Enter the Decimal Value:45
Hexadecimal Value of Decimal number is: 2D

Program in C++

### Here is the source code of the C++ program to convert a decimal number to hexadecimal using recursion.

Code:

#include<iostream>
#include<cstring>
using namespace std;
char *DecimalToHexadecimal(int n)
{
static char Hex[100];
static int rem,i=0;
if(n!=0)
{
rem=n%16;
//Convert Integer to char
if(rem<10)
Hex[i++]=rem+48;// 48 Ascii=0
else
Hex[i++]=rem+55;//55 Ascii=7
DecimalToHexadecimal(n/16);
}
return Hex;
}
int main()
{
int n;
cout<<"Enter the Decimal Value:";
cin>>n;
char *hexa;
hexa=DecimalToHexadecimal(n);
cout<<"Hexadecimal Value of Decimal number is: "<<strrev(hexa);
}

Input/Output:
Enter the Decimal Value:125
Hexadecimal Value of Decimal number is: 7D

Program in Java

### Here is the source code of the Java program to convert a decimal number to hexadecimal using recursion.

Code:

import java.util.Scanner;
public class DecimalToHexadecimalConvert {

int rem;
String str3="";
String DecimalToHexadecimal(int n)
{
if(n!=0)
{
rem=n%16;
//Convert Integer to char
if(rem<10)
str3+=(char)(rem+48);// 48 Ascii=0
else
str3+=(char)(rem+55);//55 Ascii=7
DecimalToHexadecimal(n/16);
}
return str3;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n;
System.out.print("Enter the Decimal Value:");
n=cs.nextInt();
DecimalToHexadecimalConvert ob=new DecimalToHexadecimalConvert();
String str= ob.DecimalToHexadecimal(n);
String str1="";
for(int i=str.length()-1;i>=0;i--)
{
str1+=str.charAt(i);
}
System.out.print("Hexadecimal Value of Decimal number is:"+str1);

cs.close();
}
}

Input/Output:
Enter the Decimal Value:350
Hexadecimal Value of Decimal number is:15E

Program in Python

### Here is the source code of the Python program to convert a decimal number to hexadecimal using recursion.

Code:

str3=""
def DecimalToHexadecimal(n):
global str3
if(n!=0):
rem = n % 16
if (rem < 10):
str3 += (chr)(rem + 48) # 48 Ascii = 0
else:
str3 += (chr)(rem + 55) #55 Ascii = 7
DecimalToHexadecimal(n // 16)
return str3
n=int(input("Enter the Decimal Value:"))
str=DecimalToHexadecimal(n)
print("Hexadecimal Value of Decimal number is:",''.join(reversed(str)))

Input/Output:
Enter the Decimal Value:505
Hexadecimal Value of Decimal number is: 1F9