Skip to main content

Convert Octal to Binary number

Here given code implementation process.

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

//Get a octal number and returning a decimal number
long long octal_to_decimal(long long number)
{
	long long result = 0, multiplier = 1;
	int remainder = 0;
	while (number != 0)
	{
		remainder = number % 10;
		result = (remainder * multiplier) + result;
		multiplier *= 8;
		number /= 10;
	}
	return result;
}
//Display binary of given number
void binary(long long n)
{
	long long auxiliary = n;
	int bits = 0;
	if (n < 0)
	{
		n = -n;
		auxiliary = n;
		printf("-");
	}
	while (auxiliary != 0)
	{
		auxiliary /= 10;
		bits++;
	}
	bits = bits * 3;
	if (bits > 31)
	{
		//When number exceeds the length of 4 byte integer
		return;
	}
	int flag = 0;
	for (bits; bits >= 0; bits--)
	{
		if ((n >> bits) & 1)
		{
			printf(" 1 ");
			flag = 1;
		}
		else if (flag == 1)
		{
			printf(" 0 ");
		}
	}
	printf("\n");
}
void octal_to_binary(long long number)
{
	printf("Octal : %lld  Binary : ", number);
	//Convert octal to decimal
	number = octal_to_decimal(number);
	//Convert decimal to binary
	binary(number);
}
int main()
{
	//Pass a decimal value which is contain octal value
	octal_to_binary(17);
	octal_to_binary(45);
	octal_to_binary(63);
	octal_to_binary(12);
	octal_to_binary(-45);
	return 0;
}

Output

Octal : 17  Binary :  1  1  1  1
Octal : 45  Binary :  1  0  0  1  0  1
Octal : 63  Binary :  1  1  0  0  1  1
Octal : 12  Binary :  1  0  1  0
Octal : -45  Binary : - 1  0  0  1  0  1
/*
  Java Program
  Convert Octal to Binary number
*/
class MyNumber
{
	//Get a octal number and returning a decimal number
	public long octal_to_decimal(long number)
	{
		long result = 0, multiplier = 1;
		long remainder = 0;
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		return result;
	}
	//Display binary of given number
	public void binary(long n)
	{
		if (n < 0)
		{
			n = -n;
			System.out.print("-");
		}
		long auxiliary = n;
		int size = 0;
		while (auxiliary != 0)
		{
			auxiliary = auxiliary / 10;
			size++;
		}
		size = size * 3;
		if (size > 31)
		{
			//When number exceeds the length of 4 byte integer
			return;
		}
		boolean flag = false;
		for (int bits = size; bits >= 0; bits--)
		{
			if (((n >> bits) & 1) == 0b1)
			{
				System.out.print(" 1 ");
				flag = true;
			}
			else if (flag == true)
			{
				System.out.print(" 0 ");
			}
		}
		System.out.print("\n");
	}
	public void octal_to_binary(long number)
	{
		System.out.print("Octal : " + number + "  Binary : ");
		//Convert octal to decimal
		number = octal_to_decimal(number);
		//Convert decimal to binary
		binary(number);
	}
	public static void main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		//Pass a decimal value which is indicates a octal number
		obj.octal_to_binary(17);
		obj.octal_to_binary(45);
		obj.octal_to_binary(63);
		obj.octal_to_binary(12);
		obj.octal_to_binary(-45);
	}
}

Output

Octal : 17  Binary :  1  1  1  1
Octal : 45  Binary :  1  0  0  1  0  1
Octal : 63  Binary :  1  1  0  0  1  1
Octal : 12  Binary :  1  0  1  0
Octal : -45  Binary : - 1  0  0  1  0  1
//Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  Convert Octal to Binary number
*/
class MyNumber
{
	public:
		//Get a octal number and returning a decimal number
		long octal_to_decimal(long number)
		{
			long result = 0, multiplier = 1;
			long remainder = 0;
			while (number != 0)
			{
				remainder = number % 10;
				result = (remainder * multiplier) + result;
				multiplier *= 8;
				number = number / 10;
			}
			return result;
		}
	//Display binary of given number
	void binary(long n)
	{
		if (n < 0)
		{
			n = -n;
			cout << "-";
		}
		long auxiliary = n;
		int size = 0;
		while (auxiliary != 0)
		{
			auxiliary = auxiliary / 10;
			size++;
		}
		size = size * 3;
		if (size > 31)
		{
			//When number exceeds the length of 4 byte integer
			return;
		}
		bool flag = false;
		for (int bits = size; bits >= 0; bits--)
		{
			if (((n >> bits) & 1) == 0b1)
			{
				cout << " 1 ";
				flag = true;
			}
			else if (flag == true)
			{
				cout << " 0 ";
			}
		}
		cout << "\n";
	}
	void octal_to_binary(long number)
	{
		cout << "Octal : " << number << "  Binary : ";
		//Convert octal to decimal
		number = this->octal_to_decimal(number);
		//Convert decimal to binary
		this->binary(number);
	}
};
int main()
{
	MyNumber obj = MyNumber();
	//Test Cases
	//Pass a decimal value which is indicates a octal number
	obj.octal_to_binary(17);
	obj.octal_to_binary(45);
	obj.octal_to_binary(63);
	obj.octal_to_binary(12);
	obj.octal_to_binary(-45);
	return 0;
}

Output

Octal : 17  Binary :  1  1  1  1
Octal : 45  Binary :  1  0  0  1  0  1
Octal : 63  Binary :  1  1  0  0  1  1
Octal : 12  Binary :  1  0  1  0
Octal : -45  Binary : - 1  0  0  1  0  1
//Include namespace system
using System;
/*
  C# Program
  Convert Octal to Binary number
*/
class MyNumber
{
	//Get a octal number and returning a decimal number
	public long octal_to_decimal(long number)
	{
		long result = 0, multiplier = 1;
		long remainder = 0;
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		return result;
	}
	//Display binary of given number
	public void binary(long n)
	{
		if (n < 0)
		{
			n = -n;
			Console.Write("-");
		}
		long auxiliary = n;
		int size = 0;
		while (auxiliary != 0)
		{
			auxiliary = auxiliary / 10;
			size++;
		}
		size = size * 3;
		if (size > 31)
		{
			//When number exceeds the length of 4 byte integer
			return;
		}
		Boolean flag = false;
		for (int bits = size; bits >= 0; bits--)
		{
			if (((n >> bits) & 1) == 0b1)
			{
				Console.Write(" 1 ");
				flag = true;
			}
			else if (flag == true)
			{
				Console.Write(" 0 ");
			}
		}
		Console.Write("\n");
	}
	public void octal_to_binary(long number)
	{
		Console.Write("Octal : " + number + "  Binary : ");
		//Convert octal to decimal
		number = octal_to_decimal(number);
		//Convert decimal to binary
		binary(number);
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		//Pass a decimal value which is indicates a octal number
		obj.octal_to_binary(17);
		obj.octal_to_binary(45);
		obj.octal_to_binary(63);
		obj.octal_to_binary(12);
		obj.octal_to_binary(-45);
	}
}

Output

Octal : 17  Binary :  1  1  1  1
Octal : 45  Binary :  1  0  0  1  0  1
Octal : 63  Binary :  1  1  0  0  1  1
Octal : 12  Binary :  1  0  1  0
Octal : -45  Binary : - 1  0  0  1  0  1
<?php
/*
  Php Program
  Convert Octal to Binary number
*/
class MyNumber
{
	//Get a octal number and returning a decimal number
	public	function octal_to_decimal($number)
	{
		$result = 0;
		$multiplier = 1;
		$remainder = 0;
		while ($number != 0)
		{
			$remainder = $number % 10;
			$result = ($remainder * $multiplier) + $result;
			$multiplier *= 8;
			$number = intval($number / 10);
		}
		return $result;
	}
	//Display binary of given number
	public	function binary($n)
	{
		if ($n < 0)
		{
			$n = -$n;
			echo "-";
		}
		$auxiliary = $n;
		$size = 0;
		while ($auxiliary != 0)
		{
			$auxiliary = intval($auxiliary / 10);
			$size++;
		}
		$size = $size * 3;
		if ($size > 31)
		{
			//When number exceeds the length of 4 byte integer
			return;
		}
		$flag = false;
		for ($bits = $size; $bits >= 0; $bits--)
		{
			if ((($n >> $bits) & 1) == 0b1)
			{
				echo " 1 ";
				$flag = true;
			}
			else if ($flag == true)
			{
				echo " 0 ";
			}
		}
		echo "\n";
	}
	public	function octal_to_binary($number)
	{
		echo "Octal : ". $number ."  Binary : ";
		//Convert octal to decimal
		$number = $this->octal_to_decimal($number);
		//Convert decimal to binary
		$this->binary($number);
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Cases
	//Pass a decimal value which is indicates a octal number
	$obj->octal_to_binary(17);
	$obj->octal_to_binary(45);
	$obj->octal_to_binary(63);
	$obj->octal_to_binary(12);
	$obj->octal_to_binary(-45);
}
main();

Output

Octal : 17  Binary :  1  1  1  1
Octal : 45  Binary :  1  0  0  1  0  1
Octal : 63  Binary :  1  1  0  0  1  1
Octal : 12  Binary :  1  0  1  0
Octal : -45  Binary : - 1  0  0  1  0  1
/*
  Node Js Program
  Convert Octal to Binary number
*/
class MyNumber
{
	//Get a octal number and returning a decimal number
	octal_to_decimal(number)
	{
		var result = 0;
		var multiplier = 1;
		var remainder = 0;
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = parseInt(number / 10);
		}
		return result;
	}
	//Display binary of given number
	binary(n)
	{
		if (n < 0)
		{
			n = -n;
			process.stdout.write("-");
		}
		var auxiliary = n;
		var size = 0;
		while (auxiliary != 0)
		{
			auxiliary = parseInt(auxiliary / 10);
			size++;
		}
		size = size * 3;
		if (size > 31)
		{
			//When number exceeds the length of 4 byte integer
			return;
		}
		var flag = false;
		for (var bits = size; bits >= 0; bits--)
		{
			if (((n >> bits) & 1) == 0b1)
			{
				process.stdout.write(" 1 ");
				flag = true;
			}
			else if (flag == true)
			{
				process.stdout.write(" 0 ");
			}
		}
		process.stdout.write("\n");
	}
	octal_to_binary(number)
	{
		process.stdout.write("Octal : " + number + "  Binary : ");
		//Convert octal to decimal
		number = this.octal_to_decimal(number);
		//Convert decimal to binary
		this.binary(number);
	}
}

function main()
{
	var obj = new MyNumber();
	//Test Cases
	//Pass a decimal value which is indicates a octal number
	obj.octal_to_binary(17);
	obj.octal_to_binary(45);
	obj.octal_to_binary(63);
	obj.octal_to_binary(12);
	obj.octal_to_binary(-45);
}
main();

Output

Octal : 17  Binary :  1  1  1  1
Octal : 45  Binary :  1  0  0  1  0  1
Octal : 63  Binary :  1  1  0  0  1  1
Octal : 12  Binary :  1  0  1  0
Octal : -45  Binary : - 1  0  0  1  0  1
#   Python 3 Program
#   Convert Octal to Binary number

class MyNumber :
    # Get a octal number and returning a decimal number
    def octal_to_decimal(self, number) :
        result = 0
        multiplier = 1
        remainder = 0
        while (number != 0) :
            remainder = number % 10
            result = (remainder * multiplier) + result
            multiplier *= 8
            number = int(number / 10)
        
        return result
    
    # Display binary of given number
    def binary(self, n) :
        if (n < 0) :
            n = -n
            print("-", end = "")
        
        auxiliary = n
        size = 0
        while (auxiliary != 0) :
            auxiliary = int(auxiliary / 10)
            size += 1
        
        size = size * 3
        if (size > 31) :
            # When number exceeds the length of 4 byte integer
            return
        
        flag = False
        bits = size
        while (bits >= 0) :
            if (((n >> bits) & 1) == 0b1) :
                print(" 1 ", end = "")
                flag = True
            
            elif(flag == True) :
                print(" 0 ", end = "")
            
            bits -= 1
        
        print("\n", end = "")
    
    def octal_to_binary(self, number) :
        print("Octal : ", number ,"  Binary : ", end = "")
        temp_number = number
        if (number<0):
            number = -number
        # Convert octal to decimal
        number = self.octal_to_decimal(number)
        if(temp_number<0):
            number = - number
        # Convert decimal to binary
        self.binary(number)
    

def main() :
    obj = MyNumber()
    # Test Cases
    # Pass a decimal value which is indicates a octal number
    obj.octal_to_binary(17)
    obj.octal_to_binary(45)
    obj.octal_to_binary(63)
    obj.octal_to_binary(12)
    obj.octal_to_binary(-45)

if __name__ == "__main__": main()

Output

Octal :  17   Binary :  1  1  1  1
Octal :  45   Binary :  1  0  0  1  0  1
Octal :  63   Binary :  1  1  0  0  1  1
Octal :  12   Binary :  1  0  1  0
Octal :  -45   Binary : - 1  0  0  1  0  1
/*
  Scala Program
  Convert Octal to Binary number
*/
class MyNumber
{
	//Get a octal number and returning a decimal number
	def octal_to_decimal(num: Long): Long = {
		var result: Long = 0;
		var multiplier: Long = 1;
		var remainder: Long = 0;
      	var number: Long = num;
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = (number / 10).toInt;
		}
		return result;
	}
	//Display binary of given number
	def binary(number: Long): Unit = {
      	var n : Long = number;
		if (n < 0)
		{
			n = -n;
			print("-");
		}
		var auxiliary: Long = n;
		var size: Int = 0;
		while (auxiliary != 0)
		{
			auxiliary = (auxiliary / 10).toInt;
			size += 1;
		}
		size = size * 3;
		if (size > 31)
		{
			//When number exceeds the length of 4 byte integer
			return;
		}
		var flag: Boolean = false;
		var bits: Int = size;
		while (bits >= 0)
		{
			if (((n >> bits) & 1) == 1)
			{
				print(" 1 ");
				flag = true;
			}
			else if (flag == true)
			{
				print(" 0 ");
			}
			bits -= 1;
		}
		print("\n");
	}
	def octal_to_binary(number: Long): Unit = {
		print("Octal : " + number + "  Binary : ");
		//Convert octal to decimal
		var decimal = octal_to_decimal(number);
		//Convert decimal to binary
		binary(decimal);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Cases
		//Pass a decimal value which is indicates a octal number
		obj.octal_to_binary(17);
		obj.octal_to_binary(45);
		obj.octal_to_binary(63);
		obj.octal_to_binary(12);
		obj.octal_to_binary(-45);
	}
}

Output

Octal : 17  Binary :  1  1  1  1
Octal : 45  Binary :  1  0  0  1  0  1
Octal : 63  Binary :  1  1  0  0  1  1
Octal : 12  Binary :  1  0  1  0
Octal : -45  Binary : - 1  0  0  1  0  1
/*
  Swift Program
  Convert Octal to Binary number
*/
class MyNumber
{
	//Get a octal number and returning a decimal number
	func octal_to_decimal(_ n:  Int) -> Int
	{
		var result: Int = 0;
		var multiplier: Int = 1;
		var remainder: Int = 0;
        var number : Int = n;
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		return result;
	}
	//Display binary of given number
	func binary(_ number: Int)
	{
        var n = number;
		if (n < 0)
		{
			n = -n;
			print("-", terminator: "");
		}
		var auxiliary: Int = n;
		var size: Int = 0;
		while (auxiliary != 0)
		{
			auxiliary = auxiliary / 10;
			size += 1;
		}
		size = size * 3;
		if (size > 31)
		{
			//When number exceeds the length of 4 byte integer
			return;
		}
		var flag: Bool = false;
		var bits: Int = size;
		while (bits >= 0)
		{
			if (((n >> bits) & 1) == 0b1)
			{
				print(" 1 ", terminator: "");
				flag = true;
			}
			else if (flag == true)
			{
				print(" 0 ", terminator: "");
			}
			bits -= 1;
		}
		print("\n", terminator: "");
	}
	func octal_to_binary(_ number: Int)
	{
		print("Octal : ", number ,"  Binary : ", terminator: "");
		//Convert octal to decimal
		let decimal = self.octal_to_decimal(number);
		//Convert decimal to binary
		self.binary(decimal);
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Cases
	//Pass a decimal value which is indicates a octal number
	obj.octal_to_binary(17);
	obj.octal_to_binary(45);
	obj.octal_to_binary(63);
	obj.octal_to_binary(12);
	obj.octal_to_binary(-45);
}
main();

Output

Octal :  17   Binary :  1  1  1  1
Octal :  45   Binary :  1  0  0  1  0  1
Octal :  63   Binary :  1  1  0  0  1  1
Octal :  12   Binary :  1  0  1  0
Octal :  -45   Binary : - 1  0  0  1  0  1
#   Ruby Program
#   Convert Octal to Binary number

class MyNumber

	# Get a octal number and returning a decimal number
	def octal_to_decimal(number)
	
		result = 0
		multiplier = 1
		remainder = 0
		while (number != 0)
		
			remainder = number % 10
			result = (remainder * multiplier) + result
			multiplier *= 8
			number = number / 10
		end
		return result
	end
	# Display binary of given number
	def binary(n)
	
		if (n < 0)
		
			n = -n
			print("-")
		end
		auxiliary = n
		size = 0
		while (auxiliary != 0)
		
			auxiliary = auxiliary / 10
			size += 1
		end
		size = size * 3
		if (size > 31)
		
			# When number exceeds the length of 4 byte integer
			return
		end
		flag = false
		bits = size
		while (bits >= 0)
		
			if (((n >> bits) & 1) == 0b1)
			
				print(" 1 ")
				flag = true
			elsif(flag == true)
			
				print(" 0 ")
			end
			bits -= 1
		end
		print("\n")
	end
	def octal_to_binary(number)
	
		print("Octal : ", number ,"  Binary : ")
		temp = number
		if (number<0)
        	number = -number
		end
		# Convert octal to decimal
		number = self.octal_to_decimal(number)
		if(temp<0)
        	number = -number
        end
		# Convert decimal to binary
		self.binary(number)
	end
end
def main()

	obj = MyNumber.new()
	# Test Cases
	# Pass a decimal value which is indicates a octal number
	obj.octal_to_binary(17)
	obj.octal_to_binary(45)
	obj.octal_to_binary(63)
	obj.octal_to_binary(12)
	obj.octal_to_binary(-45)
end
main()

Output

Octal : 17  Binary :  1  1  1  1 
Octal : 45  Binary :  1  0  0  1  0  1 
Octal : 63  Binary :  1  1  0  0  1  1 
Octal : 12  Binary :  1  0  1  0 
Octal : -45  Binary : - 1  0  0  1  0  1 




Comment

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