1public class DecimalToHexExample2{
2public static String toHex(int decimal){
3 int rem;
4 String hex="";
5 char hexchars[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
6 while(decimal>0)
7 {
8 rem=decimal%16;
9 hex=hexchars[rem]+hex;
10 decimal=decimal/16;
11 }
12 return hex;
13}
14public static void main(String args[]){
15 System.out.println("Hexadecimal of 10 is: "+toHex(10));
16 System.out.println("Hexadecimal of 15 is: "+toHex(15));
17 System.out.println("Hexadecimal of 289 is: "+toHex(289));
18}}
1import java.util.Scanner;
2public class DecimalToHexaExample
3{
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 System.out.println("Please enter decimal number: ");
8 int decimal = sc.nextInt();
9 String strHexadecimal = "";
10 while(decimal != 0)
11 {
12 int hexNumber = decimal % 16;
13 char charHex;
14 if(hexNumber <= 9 && hexNumber >= 0)
15 {
16 charHex = (char)(hexNumber + '0');
17 }
18 else
19 {
20 charHex = (char)(hexNumber - 10 + 'A');
21 }
22 strHexadecimal = charHex + strHexadecimal;
23 decimal = decimal / 16;
24 }
25 System.out.println("Hexadecimal number: " + strHexadecimal);
26 sc.close();
27 }
28}