Binary to Octal

Java Programming

Java Programming Examples

/* Using single Inheritance */
import java.io.*;
import java.util.Scanner;

class todecimal {

	/* Global variables */
	int decimal;
	todecimal(int binary) {
		int reminder, i = 1;
		while(binary != 0) {
			reminder = binary % 10;
			decimal += reminder * i;
			i *= 2;
			binary /= 10;
		}
		System.out.println("Decimal: "+decimal);
	}
}

/* Inherit parent class into child class using 'extends' keyword */
class tooctal extends todecimal {
	int octal, i = 1;
	tooctal(int binary) {
		super(binary);
		while(decimal != 0) {
			octal += (decimal % 8) *i;
			i *= 10;
			decimal /= 8;
		}
		System.out.println("Octal: "+octal);
	}
}

class converter {
	public static void main(String args[ ]) {
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter a Binary number");
		tooctal octal = new tooctal(Integer.parseInt(input.nextLine()));
	}
}


          /****** Output ******/
          

Please enter a Binary number


1000


Decimal: 8


Octal: 10

Learn more about Binary to Octal