Question

Python Programming Instructions Octal numbers have a base of eight and the digits 0–7. Write the...

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

Homework Answers

Answer #1
#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)
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT