java

Java Program to Convert Decimal to Hexadecimal

In this tutorial, we are going to see how to write a Java program to convert decimal to hexadecimal using the toHexString() method. The toHexString() method is a built-in function in Java that returns a string representation of the argument as an unsigned integer in base 16.
 

Program to Convert Decimal to Hexadecimal in Java :
import java.util.Scanner;

class Main
{
    public static void main(String args[])
    {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter a decimal number : ");
      //read the decimal number
      int nbr = sc.nextInt();

      String str = Integer.toHexString(nbr);
      System.out.println("The hexadecimal number is : "+str);
    }
}

Output:

Enter a decimal number : 15
The hexadecimal number is : f
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *