In the previous article, we have discussed Python Program to Find out How many 1 and 0 in a Given Number.
Given a decimal number and the task is to get the respective octal number using the while loop in Python.
Examples:
Example1:
Input:
Given Number = 15
Output:
The Octal value of the given decimal number { 15 } is: 17
Example2:
Input:
Given Number = 25
Output:
The Octal value of the given decimal number { 25 } is: 31
- Python Program to Convert Octal to Binary
- Python Program to Convert Hexadecimal to Decimal
- Python Program to Convert Hexadecimal To Octal
Program to Convert Decimal to Octal using While loop in Python
Below are the ways to convert the given decimal number into octal :
Method #1: Using While Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Take a variable say ‘a ‘and initialize its value with 1.
- Take another variable say octl_num and initialize its value with 0.
- Loop till the given number is not equal to zero using the while loop.
- Multiply ‘a’ with the given number modulus 8 and store it in a variable say ‘b‘.
- Add the above-got b to the octl_num and store it in the same variable octl_num.
- Calculate the value of the given number divided by 8.
- Store it in the same variable gvn_numb.
- Multiply a with 10 and Store it in the same variable ‘a’.
- Print the octl_num to get the octal value of the given decimal number.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 15 # Take a variable say 'a 'and initialize its value with 1. a = 1 print( "The Octal value of the given decimal number {", gvn_numb, "} is: ") # Take another variable say octl_num and initialize its value with 0. octl_num = 0 # Loop till the given number not equals to zero using the while loop. while(gvn_numb != 0): # Multiply 'a' with the given number modulus 8 and store it in a variable say 'b'. b = (gvn_numb % 8)*a # Add the above-got b to the octl_num and store it in the same variable octl_num. octl_num = octl_num+b # Calculate the value of the given number divided by 8. # Store it in the same variable gvn_numb. gvn_numb = gvn_numb//8 # Multiply a with 10 and Store it in the same variable 'a'. a = int(a*10) # Print the octl_num to get the octal value of the given decimal number. print(octl_num)
Output:
The Octal value of the given decimal number { 15 } is: 17
Method #2: Using While loop (User Input)
Approach:
- Give the 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 1.
- Take another variable say octl_num and initialize its value with 0.
- Loop till the given number is not equal to zero using the while loop.
- Multiply ‘a’ with the given number modulus 8 and store it in a variable say ‘b‘.
- Add the above-got b to the octl_num and store it in the same variable octl_num.
- Calculate the value of the given number divided by 8.
- Store it in the same variable gvn_numb.
- Multiply a with 10 and Store it in the same variable ‘a’.
- Print the octl_num to get the octal value of the given decimal number.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. gvn_numb = int(input("Enter some random number = ")) # Take a variable say 'a 'and initialize its value with 1. a = 1 print( "The Octal value of the given decimal number {", gvn_numb, "} is: ") # Take another variable say octl_num and initialize its value with 0. octl_num = 0 # Loop till the given number not equals to zero using the while loop. while(gvn_numb != 0): # Multiply 'a' with the given number modulus 8 and store it in a variable say 'b'. b = (gvn_numb % 8)*a # Add the above-got b to the octl_num and store it in the same variable octl_num. octl_num = octl_num+b # Calculate the value of the given number divided by 8. # Store it in the same variable gvn_numb. gvn_numb = gvn_numb//8 # Multiply a with 10 and Store it in the same variable 'a'. a = int(a*10) # Print the octl_num to get the octal value of the given decimal number. print(octl_num)
Output:
Enter some random number = 25 The Octal value of the given decimal number { 25 } is: 31
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.