Posted on by Kalkicode
Code Conversion

Convert octal to decimal number

The problem at hand is about converting octal numbers to decimal numbers using a program. Octal and decimal are two different numeral systems used to represent numbers. Octal is a base-8 system, meaning it uses 8 distinct digits (0-7), while decimal is a base-10 system that uses 10 digits (0-9).

Problem Statement

The task is to write a program that takes an octal number as input and converts it into its equivalent decimal representation. The program should handle both positive and negative octal numbers.

Example and Explanation

Let's take the octal number 42 as an example. In octal, the digit '4' represents the value 4 and the digit '2' represents the value 2. To convert this octal number to decimal, we need to multiply each digit by 8 raised to the power of its position (from right to left) and then sum up these values.

  • For the digit '2' at the rightmost position: 2 * 8^0 = 2
  • For the digit '4' at the next position: 4 * 8^1 = 32

Adding these values gives us 2 + 32 = 34. Therefore, the octal number 42 is equivalent to the decimal number 34.

Idea to Solve

To solve this problem, we can follow these steps:

  1. Take the input octal number.
  2. Extract each digit from the octal number using the modulo operation (%).
  3. Multiply each digit by the appropriate power of 8 (based on its position) and accumulate the results.
  4. Repeat steps 2 and 3 until all digits are processed.
  5. Print the accumulated result as the equivalent decimal number.

Standard Pseudocode

function octal_to_decimal(number):
    Initialize result to 0
    Initialize multiplier to 1
    Initialize remainder to 0
    Initialize flag to 0
    
    Print "Octal:", number
    
    if number < 0:
        Set number to -number
        Set flag to 1
    
    while number != 0:
        Calculate remainder by number % 10
        Update result by (remainder * multiplier) + result
        Update multiplier by multiplier * 8
        Update number by number / 10
    
    if flag == 1:
        Set result to -result
    
    Print "Decimal:", result

Algorithm Explanation

  1. Initialize variables: result to store the decimal result, multiplier to keep track of the position's weight (8's power), remainder to store the remainder when dividing the octal number, and flag to determine if the number is negative.
  2. Print the original octal number.
  3. If the number is negative, update the flag and make the number positive for processing.
  4. Enter a loop that runs until the number becomes zero.
  5. Inside the loop, calculate the remainder when the number is divided by 10. This gives you the rightmost digit of the octal number.
  6. Update the result by adding the product of remainder and multiplier to it. Multiply multiplier by 8 to move to the next position.
  7. Update the number by dividing it by 10 to remove the rightmost digit.
  8. After the loop, if the original number was negative (flag is 1), negate the result to get the correct negative decimal value.
  9. Print the calculated decimal result.

Program List

//C Program
//Convert octal to decimal number
#include <stdio.h>

void octal_to_decimal(int number)
{
	int result = 0, multiplier = 1;
	int remainder = 0;
	int flag = 0;
	printf(" Octal : %d ", number);
	if (number < 0)
	{
		//When number is negative number
		number = -number;
		flag = 1;
	}
	while (number != 0)
	{
		remainder = number % 10;
		result = (remainder * multiplier) + result;
		multiplier *= 8;
		number /= 10;
	}
	if (flag == 1)
	{
		result = -result;
	}
	printf(" Decimal : %d \n\n", result);
}
int main()
{
	//Test Cases
	octal_to_decimal(42);
	octal_to_decimal(18);
	octal_to_decimal(25);
	octal_to_decimal(-173);
	return 0;
}

Output

 Octal : 42  Decimal : 34

 Octal : 18  Decimal : 16

 Octal : 25  Decimal : 21

 Octal : -173  Decimal : -123
/*
  Java Program
  Convert octal to decimal number
*/
class MyNumber
{
	public void octal_to_decimal(int number)
	{
		int result = 0, multiplier = 1;
		int remainder = 0;
		int flag = 0;
		System.out.print(" Octal : " + number );
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		if (flag == 1)
		{
			result = -result;
		}
		System.out.print(" Decimal : "+ result +" \n\n");
	}
	public static void main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		obj.octal_to_decimal(42);
		obj.octal_to_decimal(18);
		obj.octal_to_decimal(25);
		obj.octal_to_decimal(-173);
	}
}

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
//Include header file
#include <iostream>
using namespace std;

/*
  C++ Program
  Convert octal to decimal number
*/

class MyNumber
{
	public: void octal_to_decimal(int number)
	{
		int result = 0, multiplier = 1;
		int remainder = 0;
		int flag = 0;
		cout << " Octal : " << number;
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		if (flag == 1)
		{
			result = -result;
		}
		cout << " Decimal : " << result << " \n\n";
	}
};
int main()
{
	MyNumber obj = MyNumber();
	//Test Cases
	obj.octal_to_decimal(42);
	obj.octal_to_decimal(18);
	obj.octal_to_decimal(25);
	obj.octal_to_decimal(-173);
	return 0;
}

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
//Include namespace system
using System;
/*
  C# Program
  Convert octal to decimal number
*/
class MyNumber
{
	public void octal_to_decimal(int number)
	{
		int result = 0, multiplier = 1;
		int remainder = 0;
		int flag = 0;
		Console.Write(" Octal : " + number);
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		if (flag == 1)
		{
			result = -result;
		}
		Console.Write(" Decimal : " + result + " \n\n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		obj.octal_to_decimal(42);
		obj.octal_to_decimal(18);
		obj.octal_to_decimal(25);
		obj.octal_to_decimal(-173);
	}
}

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
<?php
/*
  Php Program
  Convert octal to decimal number
*/
class MyNumber
{
	public	function octal_to_decimal($number)
	{
		$result = 0;
		$multiplier = 1;
		$remainder = 0;
		$flag = 0;
		echo " Octal : ". $number;
		if ($number < 0)
		{
			//When number is negative number
			$number = -$number;
			$flag = 1;
		}
		while ($number != 0)
		{
			$remainder = $number % 10;
			$result = ($remainder * $multiplier) + $result;
			$multiplier *= 8;
			$number = intval($number / 10);
		}
		if ($flag == 1)
		{
			$result = -$result;
		}
		echo " Decimal : ". $result ." \n\n";
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Cases
	$obj->octal_to_decimal(42);
	$obj->octal_to_decimal(18);
	$obj->octal_to_decimal(25);
	$obj->octal_to_decimal(-173);
}
main();

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
/*
  Node Js Program
  Convert octal to decimal number
*/
class MyNumber
{
	octal_to_decimal(number)
	{
		var result = 0;
		var multiplier = 1;
		var remainder = 0;
		var flag = 0;
		process.stdout.write(" Octal : " + number);
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = parseInt(number / 10);
		}
		if (flag == 1)
		{
			result = -result;
		}
		process.stdout.write(" Decimal : " + result + " \n\n");
	}
}

function main()
{
	var obj = new MyNumber();
	//Test Cases
	obj.octal_to_decimal(42);
	obj.octal_to_decimal(18);
	obj.octal_to_decimal(25);
	obj.octal_to_decimal(-173);
}
main();

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
#   Python 3 Program
#   Convert octal to decimal number

class MyNumber :
	def octal_to_decimal(self, number) :
		result = 0
		multiplier = 1
		remainder = 0
		flag = 0
		print(" Octal : ", number, end = "")
		if (number < 0) :
			# When number is negative number
			number = -number
			flag = 1
		
		while (number != 0) :
			remainder = number % 10
			result = (remainder * multiplier) + result
			multiplier *= 8
			number = int(number / 10)
		
		if (flag == 1) :
			result = -result
		
		print(" Decimal : ", result ," \n")
	

def main() :
	obj = MyNumber()
	# Test Cases
	obj.octal_to_decimal(42)
	obj.octal_to_decimal(18)
	obj.octal_to_decimal(25)
	obj.octal_to_decimal(-173)

if __name__ == "__main__": main()

Output

 Octal :  42 Decimal :  34

 Octal :  18 Decimal :  16

 Octal :  25 Decimal :  21

 Octal :  -173 Decimal :  -123
#   Ruby Program
#   Convert octal to decimal number

class MyNumber

	def octal_to_decimal(number)
	
		result = 0
		multiplier = 1
		remainder = 0
		flag = 0
		print(" Octal : ", number)
		if (number < 0)
		
			# When number is negative number
			number = -number
			flag = 1
		end
		while (number != 0)
		
			remainder = number % 10
			result = (remainder * multiplier) + result
			multiplier *= 8
			number = number / 10
		end
		if (flag == 1)
		
			result = -result
		end
		print(" Decimal : ", result ," \n\n")
	end
end
def main()

	obj = MyNumber.new()
	# Test Cases
	obj.octal_to_decimal(42)
	obj.octal_to_decimal(18)
	obj.octal_to_decimal(25)
	obj.octal_to_decimal(-173)
end
main()

Output

 Octal : 42 Decimal : 34 

 Octal : 18 Decimal : 16 

 Octal : 25 Decimal : 21 

 Octal : -173 Decimal : -123 

/*
  Scala Program
  Convert octal to decimal number
*/
class MyNumber
{
	def octal_to_decimal(n : Int): Unit = {
        var number = n;
		var result: Int = 0;
		var multiplier: Int = 1;
		var remainder: Int = 0;
		var flag: Int = 0;
		print(" Octal : " + number);
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = (number / 10).toInt;
		}
		if (flag == 1)
		{
			result = -result;
		}
		print(" Decimal : " + result + " \n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Cases
		obj.octal_to_decimal(42);
		obj.octal_to_decimal(18);
		obj.octal_to_decimal(25);
		obj.octal_to_decimal(-173);
	}
}

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
/*
  Swift Program
  Convert octal to decimal number
*/
class MyNumber
{
	func octal_to_decimal(_ n:  Int)
	{
      	var number: Int = n;
		var result: Int = 0;
		var multiplier: Int = 1;
		var remainder: Int = 0;
		var flag: Int = 0;
		print(" Octal : ", number, terminator: "");
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		if (flag == 1)
		{
			result = -result;
		}
		print(" Decimal : ", result ," \n\n", terminator: "");
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Cases
	obj.octal_to_decimal(42);
	obj.octal_to_decimal(18);
	obj.octal_to_decimal(25);
	obj.octal_to_decimal(-173);
}
main();

Output

 Octal :  42 Decimal :  34

 Octal :  18 Decimal :  16

 Octal :  25 Decimal :  21

 Octal :  -173 Decimal :  -123

Time Complexity

The time complexity of the provided code is determined by the number of digits in the octal number, which is roughly O(log N), where N is the value of the octal number. This is because the loop iterates through each digit of the number, performing constant time operations for each digit.

In the worst case, the number of digits in an octal number N is proportional to the logarithm base 10 of N, which gives us the time complexity of O(log N).

This algorithm is efficient and can handle octal numbers of varying lengths with ease.

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