# Python Program to Convert Octal to Decimal using While Loop

In the previous article, we have discussed Python Program to Convert Binary to Decimal using While Loop
Given an octal number and the task is to get the respective decimal number using the while loop in Python.

Examples:

Example1:

Input:

Given Octal Number = 15

Output:

The Decimal value of the given Octal number { 15 } is:
13

Example2:

Input:

Given Octal Number = 32

Output:

The Decimal value of the given Octal number { 32 } is:
26

## Program to Convert Octal to Decimal in Python

Below are the ways to convert the given octal number into decimal :

### Method #1: Using While Loop (Static Input)

Approach:

• Give the octal number as static input and store it in a variable.
• Take a variable say ‘a ‘and initialize its value with 0.
• Take another variable say deciml_numb and initialize its value with 0.
• Loop till the given octal number is not equal to zero using the while loop.
• Inside the loop, calculate the value of the given octal number modulus 10 (to get the last digit) and store it in a variable say ‘b’.
• Calculate the value of 8 raised to the power ‘a’ using the pow() function and multiply it with the above-obtained ‘b’.
• Store it in another variable ‘c’.
• Add the above variable ‘c’ with theÂ deciml_numb and store it in the same variable deciml_numb.
• Increment the value a by 1 and store it in the same variable ‘a’.
• Divide the given octal number by 10 (to remove the last digit) and store it in the same variable gvn_octl_numb.
• Print the deciml_numb to get the decimal value of the given octal number.
• The Exit of the Program.

Below is the implementation:

# Give the octal number as static input and store it in a variable.
gvn_octl_numb = 15
# Take a variable say 'a 'and initialize its value with 0.
a = 0
# Take another variable say deciml_numb and initialize its value with 0.
deciml_numb = 0
# Loop till the given octal number is not equal to zero using the while loop.
print("The Decimal value of the given Octal number {", gvn_octl_numb, "} is: ")
while(gvn_octl_numb != 0):
# Inside the loop, calculate the value of the given octal number modulus 10
# (to get the last digit) and store it in a variable say 'b'.
b = (gvn_octl_numb % 10)
# Calculate the value of 8 raised to the power 'a' using the pow() function and multiply
# it with the above-obtained 'b'.
# Store it in another variable 'c'.
c = b*pow(8, a)
# Add the above variable 'c' with theÂ deciml_numb and store it in the same variable
# deciml_numb.
deciml_numb = deciml_numb+c
# Increment the value a by 1 and store it in the same variable 'a'.
a += 1
# Divide the given octal number by 10 (to remove the last digit) and store it in the
# same variable gvn_octl_numb.
gvn_octl_numb = gvn_octl_numb // 10
# Print the deciml_num to get the decimal value of the given Octal number.
print(deciml_numb)


Output:

The Decimal value of the given Octal number { 15 } is:
13

### Method #2: Using While loop (User Input)

Approach:

• Give the octal number as user input using the int(input()) function and store it in a variable.
• Take a variable say ‘a ‘and initialize its value with 0.
• Take another variable say deciml_numb and initialize its value with 0.
• Loop till the given octal number is not equal to zero using the while loop.
• Inside the loop, calculate the value of the given octal number modulus 10 (to get the last digit) and store it in a variable say ‘b’.
• Calculate the value of 8 raised to the power ‘a’ using the pow() function and multiply it with the above-obtained ‘b’.
• Store it in another variable ‘c’.
• Add the above variable ‘c’ with theÂ deciml_numb and store it in the same variable deciml_numb.
• Increment the value a by 1 and store it in the same variable ‘a’.
• Divide the given octal number by 10 (to remove the last digit) and store it in the same variable gvn_octl_numb.
• Print the deciml_numb to get the decimal value of the given octal number.
• The Exit of the Program.

Below is the implementation:

# Give the octal number as user input using the int(input()) function and
# store it in a variable.
gvn_octl_numb = int(input("Enter some Random Number = "))
# Take a variable say 'a 'and initialize its value with 0.
a = 0
# Take another variable say deciml_numb and initialize its value with 0.
deciml_numb = 0
# Loop till the given octal number is not equal to zero using the while loop.
print("The Decimal value of the given Octal number {", gvn_octl_numb, "} is: ")
while(gvn_octl_numb != 0):
# Inside the loop, calculate the value of the given octal number modulus 10
# (to get the last digit) and store it in a variable say 'b'.
b = (gvn_octl_numb % 10)
# Calculate the value of 8 raised to the power 'a' using the pow() function and multiply
# it with the above-obtained 'b'.
# Store it in another variable 'c'.
c = b*pow(8, a)
# Add the above variable 'c' with theÂ deciml_numb and store it in the same variable
# deciml_numb.
deciml_numb = deciml_numb+c
# Increment the value a by 1 and store it in the same variable 'a'.
a += 1
# Divide the given octal number by 10 (to remove the last digit) and store it in the
# same variable gvn_octl_numb.
gvn_octl_numb = gvn_octl_numb // 10
# Print the deciml_num to get the decimal value of the given Octal number.
print(deciml_numb)


Output:

Enter some Random Number = 32
The Decimal value of the given Octal number { 32 } is:
26

Explore more instances related to python concepts fromÂ Python Programming ExamplesÂ Guide and get promoted from beginner to professional programmer level in Python Programming Language.