codescracker


java

Java Program to Convert Binary to Octal



« Previous Program Next Program »


Convert Binary to Octal

To convert binary to octal in Java programming, you have to ask to the user to enter the number in binary format to convert it into the equivalent octal format to display the equivalent octal value on the output screen.

Java Programming Code to Convert Binary to Octal

Following Java Program ask to the user to enter any number in binary format to convert it into octal format and display the result on the screen which is the same number as user enter but in octal format instead of binary:

/* Java Program Example - Binary to Octal Conversion */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int binnum, rem, quot, i=1, j;
        int octnum[] = new int[100];
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Binary Number : ");
        binnum = scan.nextInt();
		
        quot = binnum;
		
        while(quot != 0)
        {
            octnum[i++] = quot%8;
            quot = quot/8;
        }
		
        System.out.print("Equivalent Octal Value of " +binnum+ " is :\n");
        for(j=i-1; j>0; j--)
        {
            System.out.print(octnum[j]);
        }
    }
}

When the above Java Program is compile and executed, it will produce the following result:

java program convert binary to octal

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test