Java Program to Convert Decimal to Binary, Octal and Hexadecimal Number

«
»
This is a Java Program to Convert Decimal to Binary, Octal and Hexadecimal Number.

Here we take input as Decimal number and convert this entered number to HexaDecimal, Octal and Binary using methods toHexString(), toOctalString() and toBinaryString() respectively.

Here is the source code of the Java Program to Convert Decimal to Binary, Octal and Hexadecimal Number. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. class Convert
  2. {
  3.     Scanner scan;
  4.     int num;
  5.     void getVal()
  6.      {
  7.           System.out.println("Decimal to HexaDecimal,Octal and Binary");
  8.           scan = new Scanner(System.in);
  9.           System.out.println("\nEnter the number :");
  10.           num = Integer.parseInt(scan.nextLine());
  11.      }
  12.     void convert()
  13.       {
  14.            String hexa = Integer.toHexString(num);
  15.            System.out.println("HexaDecimal Value is : " + hexa);
  16.            String octal = Integer.toOctalString(num);
  17.            System.out.println("Octal Value is : " + octal);
  18.            String binary = Integer.toBinaryString(num);
  19.            System.out.println("Binary Value is : " + binary);
  20.        }
  21. }
  22. class Decimal_Conversion
  23. {
  24.    public static void main(String args[])
  25.     {
  26.         Convert obj = new Convert();
  27.          obj.getVal();
  28.          obj.convert();
  29.     }
  30. }

Output:

$ javac Decimal_Conversion.java
$ java Decimal_Conversion
 
Decimal to HexaDecimal,Octal and Binary
 
Enter the number :
121
HexaDecimal Value is : 79
Octal Value is : 171
Binary Value is : 1111001

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn