Python Programming
Instructions
Octal numbers have a base of eight and the digits 0–7. Write the scripts octalToDecimal.py and decimalToOctal.py, which convert numbers between the octal and decimal representations of integers.
These scripts use algorithms that are similar to those of the binaryToDecimal and decimalToBinary scripts developed in the Section: Strings and Number Systems.
An example of octalToDecimal.py input and output is shown below:
Enter a string of octal digits: 234
The integer value is 156
An example of decimalToOctal.py input and output is shown below:
Enter a decimal integer: 27
Quotient | Remainder | Octal |
3 | 3 | 3 |
0 | 3 | 33 |
The octal representation is 33
#OctalToDecimal.py
def Decimal(n):
n=int(n)
i=0
num=0
while(n):
a=n%10 #find the last digit
if(a>7):
print("Digit should be 0-7")
return 0
num+=a*(8**i) #mul the last digit with 8 power
i+=1
n=n//10 #remove last digit from number
print("The integer value is",num)
n=input("Enter a string of octal digits:")
Decimal(n)
#DecimalToOctal.py
def Octal(n):
Quotient=int(n)
Remainder=0
Octal=0
i=0
print("Quotient Remainder Octal")
while(Quotient):
Remainder=Quotient%8
Quotient=Quotient//8
Octal=Remainder*(10**i)+Octal
i+=1
print(Quotient," ",Remainder," ",Octal)
print("The octal representation is",Octal)
n=input("Enter a string of octal digits:")
Octal(n)
Get Answers For Free
Most questions answered within 1 hours.