This is a Java Program to Convert Binary to Octal.
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 Binary from user. We define a method as convert() to convert the user’s input to Octal.
Here is the source code of the Java Program to Convert Binary to Octal. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
class Binary_Octal
{
Scanner scan;
int num;
void getVal()
{
System.out.println("Binary to Octal");
scan = new Scanner(System.in);
System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine(), 2);
}
void convert()
{
String octal = Integer.toOctalString(num);
System.out.println("Octal Value is : " + octal);
}
}
class Main_Class
{
public static void main(String... d)
{
Binary_Octal obj = new Binary_Octal();
obj.getVal();
obj.convert();
}
}
Output:
$ javac Main_Class.java $ java Main_Class Binary to Octal Enter the number : 1010 Octal Value is : 12
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 Programming Books
- Practice Programming MCQs
- Check Java Books
- Practice Information Technology MCQs
- Practice BCA MCQs