w3resource

Java: Convert a decimal number to hexadecimal number

Java Basic: Exercise-20 with Solution

Decimal to Hexadecimal Converter

Write a Java program to convert a decimal number to a hexadecimal number.

Decimal number: The decimal numeral system is the standard system for denoting integer and non-integer numbers. It is also called base-ten positional numeral system.

Hexadecimal number: Hexadecimal is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a, b, c, d, e, f) to represent values ten to fifteen.

Test Data:
Input a decimal number: 15

Pictorial Presentation: Decimal to Hexadecimal number

Java: Convert a decimal number to hexadecimal number

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise20 {
    public static void main(String args[]) {
        // Declare variables to store decimal number and remainder
        int dec_num, rem;
        
        // Initialize an empty string for the hexadecimal number
        String hexdec_num = "";
        
        // Define the hexadecimal number digits
        char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        
        // Create a Scanner object to read input from the user
        Scanner in = new Scanner(System.in);

        // Prompt the user to input a decimal number
        System.out.print("Input a decimal number: ");
        dec_num = in.nextInt();

        // Convert the decimal number to hexadecimal
        while (dec_num > 0) {
            rem = dec_num % 16;
            hexdec_num = hex[rem] + hexdec_num;
            dec_num = dec_num / 16;
        }
        
        // Display the hexadecimal representation of the decimal number
        System.out.print("Hexadecimal number is: " + hexdec_num + "\n");
    }
}

Explanation:

In the exercise above -

  • It takes a decimal number ('dec_num') as input from the user using the "Scanner" class.
  • It initializes an empty string 'hexdec_num' to store the hexadecimal representation and defines an array 'hex' containing hexadecimal digits (0-9 and A-F).
  • It enters a loop to perform the decimal-to-hexadecimal conversion:
  • In each iteration, it calculates the remainder of 'dec_num' when divided by 16 (which gives the hexadecimal digit) and appends it to the 'hexdec_num' string.
  • It then updates 'dec_num' by dividing it by 16.
  • The loop continues until 'dec_num' becomes zero, effectively converting the entire decimal number to hexadecimal.
  • After the loop, it prints the hexadecimal representation of the decimal number stored in the 'hexdec_num' string.

Sample Output:

Input a decimal number: 15                                                                                    
Hexadecimal number is : F 

Flowchart:

Flowchart: Java exercises: Convert a decimal number to hexadecimal number

Java Code Editor:

Previous: Write a Java program to convert a decimal number to binary number.
Next: Write a Java program to convert a decimal number to octal number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/basic/java-basic-exercise-20.php