Python program to add two octal numbers


An octal number is a number expressed in the base-8 numeral system. It uses digits from 0 to 7. Octal numbers are commonly used in computer science and digital systems, especially when dealing with groups of three bits.

In octal notation, each digit represents an increasing power of 8. The rightmost digit represents 8^0 (1), the next digit represents 8^1 (8), the next digit represents 8^2 (64), and so on. By combining these digits, octal numbers can represent positive integers. For example, the octal number 52 represents the decimal number as follows.

(5 * 8^1) + (2 * 8^0) = (5 * 8) + 2 = 40 + 2 = 42

Adding octal numbers manually

Now let’s consider the two octal numbers 4568 and 1238, the following is the addition process.

4 5 6
1 2 3
--------
6 0 1
--------

After performing the addition, the result will be 6018. In the same way we can implement the python code in different approaches for adding the two octal numbers. Let’s see each approach in detail.

Using manual approach

In this approach we are implementing the manual approach of adding the two octal numbers using the python code by considering and taking care of the carry occurred if any. The following is the code which can be taken as the reference.

Example

def add_octal(octal1, octal2):
   max_len = max(len(octal1), len(octal2))
   octal1 = octal1.zfill(max_len)
   octal2 = octal2.zfill(max_len)
   result = []
   carry = 0
   for i in range(max_len - 1, -1, -1):
      digit1 = int(octal1[i])
      digit2 = int(octal2[i])
      current_sum = digit1 + digit2 + carry
      result.append(str(current_sum % 8))
      carry = current_sum // 8
   if carry:
      result.append(str(carry))
   octal_result = ''.join(result[::-1])
   return octal_result
print(add_octal("456","123"))

Output

601

Using int() and oct() functions

In python we have the functions int() and oct() which are used to convert octal number into integer and integer to octal number respectively.

Example

In this example we are converting the octal numbers to decimal by using the int() function, perform the addition, and then convert the result back to octal using the oct() function

def add_octal(oct1, oct2):
   num1 = int(oct1, 8)
   num2 = int(oct2, 8)
   result = num1 + num2
   octal_result = oct(result)[2:]
   return octal_result
res = add_octal("456","123")
print(res)

Output

601

Using sum() function

The sum() function in Python is typically used to find the sum of elements in an iterable such as a list, tuple etc. However, we can leverage its functionality along with a generator expression to perform octal addition.

Example

In this example to perform the octal addition, we need to convert each octal digit to its decimal equivalent, by using a generator expression within the sum() function.

The generator expression iterates over each digit in the octal number, converts it to an integer, and multiplies it by the corresponding power of 8 and the enumerate() function is used to obtain the position (index) of each digit in the octal number, Finally, the sum() function calculates the sum of all these decimal values.

def add_octal(octal1, octal2):
   result = sum(int(digit) * 8**i for i, digit in enumerate(octal1[::-1])) + sum(int(digit) * 8**i for i, digit in enumerate(octal2[::-1]))
   octal_result = oct(result)[2:]
   return octal_result
print(add_octal("456","123"))

Output

601

Updated on: 02-Aug-2023

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements