
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
JAVA Program to Convert Binary to Octal
Binary Number − There are four types of number systems available. Binary numbers are one of them. The Binary number is basically represented with two digits i.e. one (1) and zero (0). The binary numbers are expressed as base-2 in the numeral system.
Octal Number − Octal number is also one of the number systems available. The octal number is represented with 8 digits which is from 0 to 7(0, 1, 2, 3... 7). The Octal numbers are expressed as base-8 in the numeral system.
Here we are converting the Binary number to octal number but for this we have to first convert the binary number into decimal number so that further we can convert that decimal number into octal number.
Binary Number → Decimal Number → Octal Number
In this article we will see how to convert binary to octal in Java.
To show you some instances −
Instance-1
Input Binary number is 101011. By converting into decimal = 43. Now converting it to octal = 53
Instance-2
Input Binary number is 1111111. By converting into decimal = 127 Now converting it to octal = 177
Instance-3
Input Binary number is 11001100. By converting into decimal = 204. Now converting it to octal = 314.
Algorithm
Step-1 − Get the input number either by static input or user input method.
Step-2 − We declared two user defined methods. One method is used to convert the Binary number into a Decimal number. In another method we are converting the decimal number into octal.
Step-3 − When we convert the binary into decimal, we multiply the power of 2 in increasing order so that we can get the appropriate decimal value.
Step-4 − In another user defined method we are converting the decimal value into octal value.
Step-5 − By using this method we are converting the Binary number into octal number.
Multiple Approaches
We have provided the solution in different approaches.
By User Defined Method with Static Input Values.
By User Defined Method with User Input Values.
Let’s see the program along with its output one by one.
Approach-1: By Using User Defined Method with Static Input Value
In this approach, we declare a long variable and initialize it with a binary number in the program and pass this number as parameter in a user defined method, then inside the method by using the algorithm we can convert the binary number into octal number.
Example
import java.util.*; public class Main { public static void main(String[] args){ long binary=1010; binToDec(binary); } public static void binToDec(long binaryNumber){ int decNum = 0, i = 0; while (binaryNumber > 0) { decNum += Math.pow(2, i++) * (binaryNumber % 10); binaryNumber /= 10; } System.out.println("In Decimal="+decNum); decToOct(decNum); } public static void decToOct(long decNum){ String octalString = Long.toOctalString(decNum); int octalNumber = Integer.parseInt(octalString); System.out.println("In octal="+octalNumber); } }
Output
In Decimal=10 In octal=12
Approach-2: By Using User Defined Method with User Input Value
In this approach, we declare a binary input number and pass this number as parameter in a user defined method, then inside the method by using the algorithm we can convert the binary number into hexadecimal number.
Example
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.print("Enter a binary number: "); long binary=sc.nextLong(); binToDec(binary); } public static void binToDec(long binaryNumber){ int decNum = 0, i = 0; while (binaryNumber > 0) { decNum += Math.pow(2, i++) * (binaryNumber % 10); binaryNumber /= 10; } System.out.println("In Decimal="+decNum); decToOct(decNum); } public static void decToOct(long decNum){ String octalString = Long.toOctalString(decNum); int octalNumber = Integer.parseInt(octalString); System.out.println("In octal="+octalNumber); } }
Output
Enter a binary number: 1111 In Decimal=15 In octal=17
In this article, we explored how to convert binary to octal in Java by using different approaches.
- Related Articles
- JAVA Program to Convert Octal to Binary
- Haskell Program to convert Binary to Octal
- Golang Program to convert Binary to Octal
- C++ Program to Convert Octal Number to Binary Number
- How to Convert Binary to Octal?
- Java Program to convert integer to octal
- Java Program to convert Decimal to Octal
- JAVA Program to Convert Octal to Hexadecimal
- Java Program to convert int to Octal String
- How to Convert Octal to Binary?\n
- C++ Program to Convert Binary Number to Octal and vice-versa
- Java program to convert decimal number to octal value
- Java program to convert float decimal to Octal number
- Java Program to convert decimal integer to octal number
- Java Program to convert octal number to decimal number
