# Convert Binary to Octal number

Here given code implementation process.

``````//C Program
//Convert Binary to Octal number
#include <stdio.h>

//Convert decimal number into octal
int octal(int number)
{
int result = 0, multiplier = 1;
int remainder = 0;
while (number != 0)
{
remainder = number % 8;

result = (remainder *multiplier) + result;

multiplier *= 10;
number /= 8;
}
return result;
}
void binary_to_octal(long long number)
{
int result = 0, counter = 0;
printf("Binary : %lld ", number);
//First convert binary to decimal
while (number != 0)
{
result += (number % 10) *(1 << (counter));
counter++;
number /= 10;
}
printf(" Octal : %d \n", octal(result));
}
int main()
{
//Test Case
//Pass a decimal number which is contain binary values
binary_to_octal(1111);
binary_to_octal(10111);
binary_to_octal(101011);
binary_to_octal(11011);
binary_to_octal(-1000110);
return 0;
}```
```

#### Output

``````Binary : 1111  Octal : 17
Binary : 10111  Octal : 27
Binary : 101011  Octal : 53
Binary : 11011  Octal : 33
Binary : -1000110  Octal : -106``````
``````/*
C++ Program
*/
#include<iostream>

using namespace std;
class MyNumber
{
public:
//Convert decimal number into octal
int octal(int number)
{
int result = 0, multiplier = 1;
int remainder = 0;
while (number != 0)
{
remainder = number % 8;

result = (remainder *multiplier) + result;

multiplier *= 10;
number /= 8;
}
return result;
}
void binary_to_octal(int number)
{
int result = 0, counter = 0;
cout << "Binary : " << number;
bool flag = false;
if (number < 0)
{
// When get negative number
flag = true;
number = -number;
}
//First convert binary to decimal
while (number != 0)
{
result += (number % 10) *(1 << (counter));
counter++;
number /= 10;
}
int output = this->octal(result);
if (flag == true)
{
output = -output;
}
cout << " Octal : " << output << " \n";
}
};
int main()
{
MyNumber obj =  MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_octal(1111);
obj.binary_to_octal(10111);
obj.binary_to_octal(101011);
obj.binary_to_octal(11011);
obj.binary_to_octal(-1000110);
return 0;
}```
```

#### Output

``````Binary : 1111 Octal : 17
Binary : 10111 Octal : 27
Binary : 101011 Octal : 53
Binary : 11011 Octal : 33
Binary : -1000110 Octal : -106``````
``````/*
Java Program
*/
public class MyNumber
{

//Convert decimal number into octal
public int octal(int number)
{
int result = 0, multiplier = 1;
int remainder = 0;
while (number != 0)
{
remainder = number % 8;

result = (remainder * multiplier) + result;

multiplier *= 10;
number /= 8;
}
return result;
}
public void binary_to_octal(int number)
{
int result = 0, counter = 0;

System.out.print("Binary : "+ number);

boolean flag = false ;

if(number < 0)
{
// When get negative number
flag = true;
number = -number;
}

//First convert binary to decimal
while (number != 0)
{

result += (number % 10) * (1 << (counter));

counter++;

number /= 10;
}
int output =  octal(result);

if(flag==true)
{
output = -output;
}
System.out.print(" Octal : "+output+" \n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_octal(1111);
obj.binary_to_octal(10111);
obj.binary_to_octal(101011);
obj.binary_to_octal(11011);
obj.binary_to_octal(-1000110);
}
}```
```

#### Output

``````Binary : 1111 Octal : 17
Binary : 10111 Octal : 27
Binary : 101011 Octal : 53
Binary : 11011 Octal : 33
Binary : -1000110 Octal : -106``````
``````/*
C# Program
*/
using System;
public class MyNumber
{
//Convert decimal number into octal
public int octal(int number)
{
int result = 0, multiplier = 1;
int reMainder = 0;
while (number != 0)
{
reMainder = number % 8;

result = (reMainder * multiplier) + result;

multiplier *= 10;
number /= 8;
}
return result;
}
public void binary_to_octal(int number)
{
int result = 0, counter = 0;
Console.Write("Binary : " + number);
Boolean flag = false;
if (number < 0)
{
// When get negative number
flag = true;
number = -number;
}
//First convert binary to decimal
while (number != 0)
{
result += (number % 10) * (1 << (counter));
counter++;
number /= 10;
}
int output = octal(result);
if (flag == true)
{
output = -output;
}
Console.Write(" Octal : " + output + " \n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_octal(1111);
obj.binary_to_octal(10111);
obj.binary_to_octal(101011);
obj.binary_to_octal(11011);
obj.binary_to_octal(-1000110);
}
}```
```

#### Output

``````Binary : 1111 Octal : 17
Binary : 10111 Octal : 27
Binary : 101011 Octal : 53
Binary : 11011 Octal : 33
Binary : -1000110 Octal : -106``````
``````<?php
/*
Php Program
*/
class MyNumber
{
//Convert decimal number into octal
public  function octal(\$number)
{
\$result = 0;
\$multiplier = 1;
\$remainder = 0;
while (\$number != 0)
{
\$remainder = \$number % 8;

\$result = (\$remainder *\$multiplier) + \$result;
\$multiplier *= 10;
\$number = intval(\$number / 8);
}
return \$result;
}
public  function binary_to_octal(\$number)
{
\$result = 0;
\$counter = 0;
echo("Binary : ". \$number);
\$flag = false;
if (\$number < 0)
{
// When get negative number
\$flag = true;
\$number = -\$number;
}
//First convert binary to decimal
while (\$number != 0)
{
\$result += (\$number % 10) *(1 << (\$counter));
\$counter++;
\$number = intval(\$number / 10);
}
\$output = \$this->octal(\$result);
if (\$flag == true)
{
\$output = -\$output;
}
echo(" Octal : ". \$output ." \n");
}
}

function main()
{
\$obj = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
\$obj->binary_to_octal(1111);
\$obj->binary_to_octal(10111);
\$obj->binary_to_octal(101011);
\$obj->binary_to_octal(11011);
\$obj->binary_to_octal(-1000110);
}
main();``````

#### Output

``````Binary : 1111 Octal : 17
Binary : 10111 Octal : 27
Binary : 101011 Octal : 53
Binary : 11011 Octal : 33
Binary : -1000110 Octal : -106``````
``````/*
Node Js Program
*/
class MyNumber
{
//Convert decimal number into octal
octal(number)
{
var result = 0;
var multiplier = 1;
var remainder = 0;
while (number != 0)
{
remainder = number % 8;

result = (remainder *multiplier) + result;

multiplier *= 10;
number = parseInt(number / 8);
}
return result;
}
binary_to_octal(number)
{
var result = 0;
var counter = 0;
process.stdout.write("Binary : " + number);
var flag = false;
if (number < 0)
{
// When get negative number
flag = true;
number = -number;
}
//First convert binary to decimal
while (number != 0)
{
result += (number % 10) *(1 << (counter));
counter++;
number = parseInt(number / 10);
}
var output = this.octal(result);
if (flag == true)
{
output = -output;
}
process.stdout.write(" Octal : " + output + " \n");
}
}

function main(args)
{
var obj = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_octal(1111);
obj.binary_to_octal(10111);
obj.binary_to_octal(101011);
obj.binary_to_octal(11011);
obj.binary_to_octal(-1000110);
}
main();``````

#### Output

``````Binary : 1111 Octal : 17
Binary : 10111 Octal : 27
Binary : 101011 Octal : 53
Binary : 11011 Octal : 33
Binary : -1000110 Octal : -106``````
``````#   Python 3 Program
#   Convert Binary to Hexadecimal number

class MyNumber :
# Convert decimal number into octal
def octal(self, number) :
result = 0
multiplier = 1
remainder = 0
while (number != 0) :
remainder = number % 8

result = (remainder * multiplier) + result

multiplier *= 10
number = int(number / 8)

return result

def binary_to_octal(self, number) :
result = 0
counter = 0
print("Binary : ", number, end = "")
flag = False
if (number < 0) :
#  When get negative number
flag = True
number = -number

# First convert binary to decimal
while (number != 0) :
result += (number % 10) * (1 << (counter))
counter += 1
number = int(number / 10)

output = self.octal(result)
if (flag == True) :
output = -output

print(" Octal : ", output ," \n", end = "")

def main() :
obj = MyNumber()
# Test Cases
# Pass a decimal number which is contain binary values
obj.binary_to_octal(1111)
obj.binary_to_octal(10111)
obj.binary_to_octal(101011)
obj.binary_to_octal(11011)
obj.binary_to_octal(-1000110)

if __name__ == "__main__": main()```
```

#### Output

``````Binary :  1111 Octal :  17
Binary :  10111 Octal :  27
Binary :  101011 Octal :  53
Binary :  11011 Octal :  33
Binary :  -1000110 Octal :  -106``````
``````#   Ruby Program
#   Convert Binary to Hexadecimal number

class MyNumber

# Convert decimal number into octal
def octal(number)

result = 0
multiplier = 1
remainder = 0
while (number != 0)

remainder = number % 8

result = (remainder * multiplier) + result

multiplier *= 10
number /= 8
end
return result
end
def binary_to_octal(number)

result = 0
counter = 0
print("Binary : ", number)
flag = false
if (number < 0)

#  When get negative number
flag = true
number = -number
end
# First convert binary to decimal
while (number != 0)

result += (number % 10) * (1 << (counter))
counter += 1
number /= 10
end
output = self.octal(result)
if (flag == true)

output = -output
end
print(" Octal : ", output ," \n")
end
end
def main()

obj = MyNumber.new()
# Test Cases
# Pass a decimal number which is contain binary values
obj.binary_to_octal(1111)
obj.binary_to_octal(10111)
obj.binary_to_octal(101011)
obj.binary_to_octal(11011)
obj.binary_to_octal(-1000110)
end
main()```
```

#### Output

``````Binary : 1111 Octal : 17
Binary : 10111 Octal : 27
Binary : 101011 Octal : 53
Binary : 11011 Octal : 33
Binary : -1000110 Octal : -106
``````
``````/*
Scala Program
*/
class MyNumber
{
//Convert decimal number into octal
def octal(num: Int): Int = {
var result : Int = 0;
var multiplier : Int = 1;
var remainder : Int = 0;
var number : Int = num;
while (number != 0)
{
remainder = number % 8;

result = (remainder * multiplier) + result;

multiplier *= 10;
number = (number / 8).toInt;
}
return result;
}
def binary_to_octal(num: Int): Unit = {
var result: Int = 0;
var counter: Int = 0;
var number : Int = num;
print("Binary : " + number);
var flag: Boolean = false;
if (number < 0)
{
// When get negative number
flag = true;
number = -number;
}
//First convert binary to decimal
while (number != 0)
{
result += (number % 10) * (1 << (counter));
counter += 1;
number = (number / 10).toInt;
}
var output: Int = octal(result);
if (flag == true)
{
output = -output;
}
print(" Octal : " + output + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_octal(1111);
obj.binary_to_octal(10111);
obj.binary_to_octal(101011);
obj.binary_to_octal(11011);
obj.binary_to_octal(-1000110);
}
}```
```

#### Output

``````Binary : 1111 Octal : 17
Binary : 10111 Octal : 27
Binary : 101011 Octal : 53
Binary : 11011 Octal : 33
Binary : -1000110 Octal : -106``````
``````/*
Swift Program
*/
class MyNumber
{
//Convert decimal number into octal
func octal(_ number: inout Int) -> Int
{
var result: Int = 0;
var multiplier: Int = 1;
var remainder: Int = 0;
while (number != 0)
{
remainder = number % 8;

result = (remainder * multiplier) + result;

multiplier *= 10;
number /= 8;
}
return result;
}
func binary_to_octal(_ num:  Int)
{
var result: Int = 0;
var counter: Int = 0;
var number : Int = num;
print("Binary : ", number, terminator: "");
var flag: Bool = false;
if (number < 0)
{
// When get negative number
flag = true;
number = -number;
}
//First convert binary to decimal
while (number != 0)
{
result += (number % 10) * (1 << (counter));
counter += 1;
number /= 10;
}
var output: Int = self.octal(&result);
if (flag == true)
{
output = -output;
}
print(" Octal : ", output ," \n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_octal(1111);
obj.binary_to_octal(10111);
obj.binary_to_octal(101011);
obj.binary_to_octal(11011);
obj.binary_to_octal(-1000110);
}
main();```
```

#### Output

``````Binary :  1111 Octal :  17
Binary :  10111 Octal :  27
Binary :  101011 Octal :  53
Binary :  11011 Octal :  33
Binary :  -1000110 Octal :  -106``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

### New Comment 