This is a Java Program to Convert Octal to Binary.
We make a class with two methods one for input and other for conversion and access this by object of this class. We first take the input as Octal from user. We define a method as convert() to convert the user’s input to Binary.
Here is the source code of the Java Program to Convert Octal to Binary. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
class Octal_Binary
{
Scanner scan;
int num;
void getVal()
{
System.out.println("Octal to Binary");
scan = new Scanner(System.in);
System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine(), 8);
}
void convert()
{
String binary = Integer.toBinaryString(num);
System.out.println("Binary Value is : " + binary);
}
}
class MainClass
{
public static void main(String args[])
{
Octal_Binary obj = new Octal_Binary();
obj.getVal();
obj.convert();
}
}
Output:
$ javac MainClass.java $ java MainClass Octal to Binary Enter the number : 10 Binary Value is : 1000
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]Related Posts:
- Check Java Books
- Practice Programming MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Practice Information Technology MCQs