Python program to add two Octal numbers Last Updated : 14 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Given two octal numbers, the task is to write a Python program to compute their sum. Examples: Input: a = "123", b = "456" Output: 601 Input: a = "654", b = "321" Output: 1175Approach: To add two octal values in python we will first convert them into decimal values then add them and then finally again convert them to an octal value. To convert the numbers we will make use of the oct() function. The oct() function is one of the built-in methods in Python3. The oct() method takes an integer and returns its octal representation in a string format. We will also use the int() function to convert the number to decimal form. The int() function in Python and Python3 converts a number in the given base to decimal. Below are the implementations based on the above explanation: Example 1: Python3 # Python program to add two hexadecimal numbers. # Driver code # Declaring the variables a = "123" b = "456" # Calculating octal value using function sum = oct(int(a, 8) + int(b, 8)) # Printing result print(sum[2:]) Output601 Example 2: Python3 # Python program to add two hexadecimal numbers. # Driver code # Declaring the variables a = "654" b = "321" # Calculating octal value using function sum = oct(int(a, 8) + int(b, 8)) # Printing result print(sum[2:]) Output1175 Example 3: Python3 # Python program to add two octal numbers. # Driver code if __name__ == "__main__" : # Declaring the variables a = "654" b = "321" # Calculating octal sum by using hex() and int() octal_sum = lambda a,b : oct(int(a, 8) + int(b, 8)) # calling octal lambda function print(octal_sum(a,b)[2:]) # This code is contributed by AnkThon Output1175 Time Complexity : O(1) Space Complexity : O(1) Method: Using "add" operator Python3 from operator import* num1="654" num2="321" print(oct(add(int(num1,8),int(num2,8)))) Output 0o1175 Time Complexity : O(1) Space Complexity : O(1) Comment More infoAdvertise with us Next Article Python program to add two Octal numbers A aditya_taparia Follow Improve Article Tags : Python Python Programs Numbers Practice Tags : Numberspython Similar Reads Python program to add two binary numbers Given two binary numbers, write a Python program to compute their sum. Examples: Input: a = "11", b = "1" Output: "100" Input: a = "1101", b = "100" Output: 10001Approach: Naive Approach: The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum become 4 min read Python Program to add two hexadecimal numbers Given two hexadecimal numbers, write a Python program to compute their sum. Examples: Input: a = "01B", b = "378" Output: 393 Explanation: B (11 in decimal) + 8 = 19 (13 in hex), hence addition bit = 3, carry = 1 1 + 7 + 1 (carry) = 9, hence addition bit = 9, carry = 0 0 + 3 + 0 (carry) = 3, hence a 6 min read Python program to add two matrices Prerequisite : Arrays in Python, Loops, List Comprehension Program to compute the sum of two matrices and then print it in Python. We can perform matrix addition in various ways in Python. Here are a two of them. Examples:Input : X= [[1,2,3], [4 ,5,6], [7 ,8,9]] Y = [[9,8,7], [6,5,4], [3,2,1]] Outpu 2 min read Python Program to Subtract Two Binary Numbers We are given two binary numbers, num1 and num2 and we have to subtract these two binary numbers and return the result. In this article, we will see how we can subtract two binary numbers in Python.Examples:Input: a = "1110" , b = "0110"Output: "1000"Explanation: We can see that when "1110" is subtra 3 min read Python Program to Multiply Two Binary Numbers Given two binary numbers, and the task is to write a Python program to multiply both numbers. Example: firstnumber = 110 secondnumber = 10 Multiplication Result = 1100 We can multiply two binary numbers in two ways using python, and these are: Using bin() functions andWithout using pre-defined funct 2 min read Like