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.
class Convert
{
Scanner scan;
int num;
void getVal()
{
System.out.println("Decimal to HexaDecimal,Octal and Binary");
scan = new Scanner(System.in);
System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine());
}
void convert()
{
String hexa = Integer.toHexString(num);
System.out.println("HexaDecimal Value is : " + hexa);
String octal = Integer.toOctalString(num);
System.out.println("Octal Value is : " + octal);
String binary = Integer.toBinaryString(num);
System.out.println("Binary Value is : " + binary);
}
}
class Decimal_Conversion
{
public static void main(String args[])
{
Convert obj = new Convert();
obj.getVal();
obj.convert();
}
}
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 Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice Information Technology MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship
- Check Java Books
- Apply for Java Internship