Program for Decimal to Hexadecimal Conversion in Java
Java
// Java program to convert Decimal Number // to Hexadecimal Number // Importing input output classes import java.io.*; // Main class public class GFG { // Method 1 // To convert decimal to hexadecimal static void decTohex( int n) { // Creating an array to store octal number int [] hexNum = new int [ 100 ]; // counter for hexadecimal number array int i = 0 ; while (n != 0 ) { // Storing remainder in hexadecimal array hexNum[i] = n % 16 ; n = n / 16 ; i++; } // Printing hexadecimal number array // in the reverse order for ( int j = i - 1 ; j >= 0 ; j--) { if (hexNum[j] > 9 ) System.out.print(( char )( 55 + hexNum[j])); else System.out.print(hexNum[j]); } } // Method 2 // Main driver method public static void main(String[] args) { // Custom input decimal number // to be converted into hexadecimal number int n = 2545 ; // Calling the above Method1 over number 'n' // to convert this decimal into hexadecimal number decTohex(n); } } |
Output
9F1
The complexity of the above method:
Time Complexity: O(log N)
Auxiliary Space: O(1)
Java Program For Decimal to Hexadecimal Conversion
Given a decimal number N, convert N into an equivalent hexadecimal number i.e. convert the number with base value 10 to base value 16. The decimal number system uses 10 digits 0-9 and the Hexadecimal number system uses 0-9, A-F to represent any numeric value.
Examples of Decimal to Hexadecimal Conversion
Input : 10
Output: AInput : 2545
Output: 9F1
Contact Us