java program to convert decimal to hexadecimal using recursion

Solutions on MaxInterview for java program to convert decimal to hexadecimal using recursion by the best coders in the world

showing results for - "java program to convert decimal to hexadecimal using recursion"
Giada
05 Jul 2018
1// convert decimal to hexadecimal using recursion
2import java.util.Scanner;
3class DecimalToHexaDemo
4{
5   char[] charHexa ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
6   int num;
7   String strHex = "";
8   String hexadecimal(int h)
9   {
10      if(h != 0)
11      {
12         num = h % 16;
13         strHex = charHexa[num] + strHex;
14         h = h / 16;
15         hexadecimal(h);
16      }
17      return strHex;
18   }
19   public static void main(String[] args)
20   {
21      DecimalToHexaDemo obj = new DecimalToHexaDemo();
22      int decimal;
23      Scanner sc = new Scanner(System.in);
24      System.out.println("Please enter decimal number: ");
25      decimal = sc.nextInt();
26      System.out.println("Hexadecimal number is: ");
27      String hex = obj.hexadecimal(decimal);
28      System.out.println(hex);
29      sc.close();
30   }
31}