Binary to octal conversion in python by R4R Team

Binary number

-It is a number which expressed in the base-2 numeral system,
-Binary numbers is 0 or 1

Octal number

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

Conversion of binary to octal-
Example -
Binary number is 101010
Its octal equivalent is 52

Binary number is 1111
Its octal equivalent is 17

program -

print("Enter Binary number")
n=int(input())
def func(n):
s=0
i=0
while(n):
t=n%10
s=s+t*(2**i)
n=int(n/10)
i=i+1
return s
s=0
while(n>0):
t=n%1000
s=s*10+func(t)
n=int(n/1000)

s=str(s)
s=s[::-1]
print("Octal equivalent is - "+str(s))


output -

Enter Binary number
101010
Octal equivalent is - 52

Enter Binary number
1111111
Octal equivalent is - 177


-In this program,
we take three three digit of binary number and find the decimal equivalent of each three digit and combine all decimal equivalent and reverse them so we get the octal of given binary 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!