Decimal to Octal in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The decimal system is the most widely used number system. While computers only understand binary and binary, octal and hexadecimal number systems are closely related, so we may require to convert decimal into these systems.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Introduction

Suppose we are given a decimal number as input, and we need to write an algorithm to convert the given decimal number into its octal number system representation, i.e. convert the number with base value 10 to base value 8. The base value of a number system determines the number of digits used to represent a numeric value.

Introduction

The binary number system uses two digits 0 and 1, the octal number system uses 8 digits from 0-7, and the decimal number system uses ten digits 0-9 to represent any numeric value.

A number with the prefix 0b is considered binary, 0o is considered octal, and 0x is hexadecimal.

For Example- 60 = 0b100000 = 0o40 = 0x20

Algorithm

  1. Store the remainder when the number is divided by 8 in an array.
  2. Divide the number by 8 now
  3. Repeat the above two steps until the number is not equal to 0.
  4. Print the array in reverse order now.

For Example:

If the given decimal number is 10,

Step 1: When 10 is divided by 8, the remainder is 2. Therefore, arr[0] = 2.

Step 2: Divide 10 by 8. New number is 10/8 = 1.

Step 3: When 1 is divided by 8, the remainder is 1. Therefore, arr[1] = 1.

Step 4: Divide 1 by 8. New number is 1/8 = 0.

Step 5: Since the number becomes = 0, we can stop repeating steps and print the array in reverse order.

Algorithm

Therefore, the equivalent octal number is '12'.

Decimal to Octal in Python Using Loop

The common method of calculating the octal representation of a decimal number is to divide the number by 8 until it becomes 0. After the division is over, we print the array storing the remainders at each division point in reverse order.

Calling the Function

DecimalToOctal(33)

Output:

The number of iterations in the above program is equal to the number of bits in the octal representation of the given number n, which makes the Time Complexity: O(log N)

We initialize an array at the starting of the program to store the remainders and make the Space Complexity: O(N)

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Decimal to Octal in Python Using Recursion

When we want to convert a decimal number n into octal using recursion, we have to pass the quotient (n/8) to the next recursive call and output the remainder value (n%8).

We repeat the process until the number reduces to zero. Since recursion implements stack, the remainders will get printed in a bottom-up manner, and we get the equivalent octal number.

Calculation

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Decimal to Octal in Python Using O(1) Space Complexity

This method of converting a decimal number to its octal representation divides the decimal by 8 until it reduces to 0. After the division is over, if we stack the remainders in a bottom-up manner, the result value will be the equivalent octal number.

Output

Time Complexity: O(log N)

No extra array is created for storing the remainders. That's why the Space Complexity: O(1)

In the above program, we have initialized the octal number as octal to 0, count to 1, and the decimal variable as deci to n. After this step, we calculate the remainder when the decimal number is divided by 8, the value of octal is updated by octalNum + (remainder * count), the value of count is increased by count*10, and at last the decimal number deci is divided by 8. The whole process runs until the decimal variable becomes zero.

Decimal to Octal in Python Using oct()

The built-in Python method oct() returns the equivalent octal representation of the decimal number passed as a parameter. The oct() function returns an octal number in the form of 0oxyz, where xyz is the actual octal value.

Syntax

The syntax of oct() is

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Parameters

The oct() function takes a single parameter. This parameter could be an integer number (binary, decimal, or hexadecimal).

Return Values

The oct() function returns an octal string from the given integer number.

Working of oct() in Python

Output

The above oct(15) returns 0o17 as the output, which means the actual octal value for decimal number 15 is 17.

Conclusion

  • The base value of a number system determines the number of digits used to represent a numeric value.
  • The binary number system uses two digits, 0 and 1, the octal number system uses 8 digits from 0-7 and the decimal number system uses 10 digits 0-9 to represent any numeric value.
  • Python has a built-in oct() function that returns the equivalent octal representation of the decimal number passed as a parameter.

Read More: