Decimal to octal conversion in python by R4R Team

Octal number-
- A number which expressed in the base-8 numeral system.
- Octal number range is 0 to 7

Deciaml number -
- A number which expressed in the base-10 numeral system.
- Decimal number range is 0 to 9

Conversion of Decimal to octal number
Example-
Decimal number - 10
Its octal equivalent is 1010

Decimal number - 11
Its octal equivalent is 1011

Program -

print("Enter decimal number")
n=int(input())
octal=""
l=[]

#break decimal number into octal bits
while(n>0):
t=n%8
l.append(t)
n=int(n/8)

i=len(l)-1

#traverse list in reverse order
while(i>=0):
octal=octal+str(l[i])
i=i-1

print("Its octal equivalent is - "+octal)


output -

Enter decimal number
10
Its octal equivalent is - 12


Enter decimal number
45
Its octal equivalent is - 55


-In this program, firstly we take input of decimal number then using 1st while loop, we break the decimal into octal bits and store those bit in list and in 2nd while loop, we traverse the list in reverse order and add to 'octal' variable and this variable have the octal equivalent of given number.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!