Java program to convert decimal to hexadecimal number.



package demo;
import java.util.Scanner;
public class DecimalToHexadecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int decimalNumber, remainder, quotient;
  int i = 1, temp;
  byte[] hexadecimalNumber = new byte[100];
  System.out.print("Enter any decimal number: ");
  decimalNumber = sc.nextInt();
  quotient = decimalNumber;
  while (quotient != 0) 
  {
   temp = (quotient % 16);              //To convert integer into character
   if (temp < 10) 
   {
    temp = temp + 48;
   } else 
   {
    temp = temp + 55;
   }
   hexadecimalNumber[i++] = (byte) temp;
   quotient = quotient / 16;
  }
  System.out.print("Equivalent hexadecimal value of decimal number " + ( decimalNumber) + ": ");
  for (int j = i - 1; j > 0; j--) 
  {
   System.out.print((char)(hexadecimalNumber[j]));
  }
 }
}

Output:
Enter any decimal number: 1000
Equivalent hexadecimal value of decimal number 1000: 3E8
BUILD SUCCESSFUL (total time: 3 seconds)



No comments:

Post a Comment