GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python Program to Convert Decimal to Octal
Python Program to Convert Decimal to OctalThis article is created to cover some programs in Python, to convert decimal number entered by user at run-time to its equivalent octal value. Here are the list of programs:
Note - Before creating these programs, if you're not aware about steps used for the conversion, then refer to Decimal to Octal Conversion Steps and Example to get every required things about the topic. Decimal to Octal using List and while LoopTo convert decimal to octal number in Python, you have to ask from user to enter a number in decimal number system, then convert that number into its equivalent value in octal number system as shown in the program given below. The question is, write a Python program to convert decimal to octal using list and while loop. Here is its answer: print("Enter the Decimal Number: ") decnum = int(input()) i = 0 octnum = [] while decnum!=0: rem = decnum%8 octnum.insert(i, rem) i = i+1 decnum = int(decnum/8) print("\nEquivalent Octal Value is: ") i = i-1 while i>=0: print(octnum[i], end="") i = i-1 print() Here is its sample run: Now supply the input say 346 as a decimal number, then press The dry run of above program with user input 346 goes like:
Modified Version of Previous ProgramThis is the modified version of previous program. This program uses end= to skip printing of an automatic newline using print() print("Enter Decimal Number: ", end="") dnum = int(input()) i = 0 onum = [] while dnum!=0: onum.insert(i, dnum % 8) i = i+1 dnum = int(dnum / 8) print("\nEquivalent Octal Value = ", end="") i = i-1 while i>=0: print(onum[i], end="") i = i-1 print() Here is its sample run with user input 3112 as decimal number input: Decimal to Octal using oct()The method oct() returns octal equivalent of value passed as its argument. print("Enter Decimal Number: ", end="") dnum = int(input()) onum = oct(dnum) print("\nEquivalent Octal Value = ", onum) Here is its sample run with decimal number input as 1234: Note - To skip first two characters of octal number output, add [2:] after onum while printing. That is, replace the following statement (from above program): print("\nEquivalent Octal Value = ", onum) with the statement given below: print("\nEquivalent Octal Value = ", onum[2:]) Now the output looks like: Decimal to Octal using FunctionThis program is created using user-defined and predefined functions both. The user-defined function named DecToOct() receives a value as its argument and returns its octal equivalent using oct() method. def DecToOct(d): return oct(d) print("Enter Decimal Number: ", end="") dnum = int(input()) onum = DecToOct(dnum) print("\nEquivalent Octal Value =", onum[2:]) Using the following statement: onum = DecToOct(dnum) The value of dnum variable (stores decimal number entered by user) gets passed to DecToOct() function. And this function returns its equivalent octal value. Therefore this value (octal) gets returned and initialized to onum variable. Now print the value of onum variable as octal equivalent of given decimal number. Decimal to Octal using ClassThis program is created using class, an object-oriented feature of Python. That is, a class named CodesCracker is created with a member function named DecToOct(). To access member function of a class, an object of class can be used. Therefore an object named obj is created of class CodesCracker to access its member function named DecToOct() using dot (.) operator. class CodesCracker: def DecToOct(self, d): return oct(d) print("Enter Decimal Number: ", end="") dnum = int(input()) obj = CodesCracker() onum = obj.DecToOct(dnum) print("\nEquivalent Octal Value =", onum[2:]) Same Program in Other Languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |