
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Binary to Octal in Java
Binary Number ? There are many types of number systems depending upon the base value. Binary numbers are one of them. The Binary number is basically represented by 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 an octal number but for this, we have to first convert the binary number into a decimal number so that further we can convert that decimal number into an octal number using the toOctalString() method.
Binary Number ? Decimal Number ? Octal Number
The toOctalString() method is a built-in function of the Integer class.
Syntax
public static String toOctalString(int i)
Problem Statement
Write a program in Java to convert binary numbers to octal numbers ?
Input-1
Input Binary number is 101011.
Output-1
By converting into decimal = 43. Now converting it to octal = 53
Input-2
Input Binary number is 1111111.
Output-2
By converting into decimal = 127 Now converting it to octal = 177
Input-3
Input Binary number is 11001100.
Output-3
By converting into decimal = 204. Now converting it to octal = 314.
Approaches to convert binary to octal
We have provided the solution in different approaches ?
- By using the user-defined method with Static Input Values.
- By using the user-defined method with User Input Values.
Approach-1: By Using User Defined Method with Static Input Value
In this approach, we declare a long variable initialize it with a binary number in the program, and pass this number as a parameter in a user-defined method, then inside the method by using the algorithm, we can convert the binary number into an octal number.
- Start
- Import java.util.* and define the Main class.
- Declare a long variable binary with a static value.
- Convert Binary to Decimal then convert Decimal to Octal and print the values.
- Stop
Example
Below is an example of converting the binary number into an octal number using the static input method -
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 a parameter in a user-defined method, then inside the method by using the algorithm, we can convert the binary number into a hexadecimal number.
- Start
- Import java.util.* and define the Main class.
- Take User Input for Binary Number.
- Call User-Defined Method for Conversion.
- Convert Binary to Decimal then convert Decimal to Octal and print the values.
- Stop.
Example
Below is an example of converting the binary number into an octal number using a user-defined method -
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.