Java Programming

Binary to Octal

Java programming for covert binary to octal using single inheritance

9/17/2021
0 views
binary-to-octal.javaJava
/* 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
Java programmingsingle inheritancebinary to octal

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com