Java Program to Convert Decimal to Binary, Octal and Hexadecimal

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.

  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
advertisement

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.