Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
How to Convert Decimal to Octal in Python? Program with Examples
Various number systems play a vital role in representing and manipulating data in the field of programming. While we are used to the decimal system, which is based on the number 10, computer systems often utilize different number systems to optimize data storage and processing efficiency. One such system is the octal number system.
The octal system is a base-8 number system, which means it uses eight distinct digits: 0, 1, 2, 3, 4, 5, 6, and 7. Each digit represents a unique value in powers of 8. Converting decimal to octal in Python is an essential skill for programmers, as it allows them to work with and interpret octal representations of data within various programming domains.
In this post, we will learn how to convert decimal to octal in Python. We will provide a step-by-step guide, complete with examples, to help you grasp the concept and apply it effectively in your programming projects.
Understanding decimal-to-octal conversion not only enhances your understanding of number systems but also equips you with valuable skills in low-level programming, data storage optimization, and computer networking. Additionally, octal numbers find applications in areas such as file permissions, flag settings, and hardware registers.
By learning how to convert decimal numbers to octal in Python, you will gain a versatile skill in your programming.
Python Program to Convert Octal to Decimal
Code
# Program to convert decimal to octal in Python
def decimal_to_octal(decimal):
octal = ""
while decimal > 0:
octal = str(decimal % 8) + octal
decimal = decimal // 8
return octal
if __name__ == "__main__":
decimal = int(input("Enter a decimal number: "))
octal = decimal_to_octal(decimal)
print("Octal: " + octal)
Output
Enter a decimal number: 64
Octal: 100
Explanation
The decimal_to_octal() function takes a decimal number as input and returns its octal representation as a string.
Inside the function, we initialize an empty string octal to store the octal digits.
Using a while loop, we repeatedly perform the following steps until the decimal number becomes zero:
-
We compute the remainder when the decimal number is divided by 8 using the modulus operator %.
-
We convert the remainder to a string and concatenate it with the left side of the octal string.
-
We update the decimal number by performing integer division by 8 (decimal // 8).
After the while loop ends, we have obtained the octal representation of the decimal number.
Finally, we return the resulting octal string.
In the main program, we prompt the user to enter a decimal number using the input() function, and we convert it to an integer using int().
We call the decimal_to_octal() function with the decimal number as an argument and store the result in the octal variable.
Finally, we print the octal representation of the decimal number using the print() function, along with an appropriate label.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program