Java program to convert decimal to hexadecimal using recursion

Given a decimal number, we have to convert it to hexadecimal using recursion.
Submitted by Nidhi, on June 03, 2022

Problem Solution:

In this program, we will read an integer number from the user, and then we will convert it to an equivalent hexadecimal number using recursion.

Program/Source Code:

The source code to convert decimal to hexadecimal using recursion is given below. The given program is compiled and executed successfully.

// Java program to convert decimal to hexadecimal 
// using the recursion

import java.util.*;

public class Main {
  static char[] hexChar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  static String strHex = "";
  static int num = 0;

  public static String decToHex(int dec) {
    if (dec != 0) {
      num = dec % 16;
      strHex = hexChar[num] + strHex;
      dec = dec / 16;
    return strHex;

  public static void main(String[] args) {
    Scanner X = new Scanner(;

    int num = 0;
    String res;

    System.out.printf("Enter number: ");
    num = X.nextInt();

    res = decToHex(num);
    System.out.printf("Hex-Decimal number is: " + res);


Enter number: 234
Hex-Decimal number is: EA


In the above program, we imported the "java.util.*" package to use the Scanner class. Here, we created a public class Main. The Main class contains two static methods decToHex() and main(). The decToHex() is a recursive method that converts a decimal number into a hexadecimal number and returns the result to the calling method.

The main() method is the entry point for the program. Here, we read two integer numbers from the user and called the decToHex() method to convert the decimal number to a hexadecimal number and printed the result.

Java Recursion Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.