
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Check if number is palindrome or not in Octal in Python
Suppose we have a number which is either in octal or in decimal form. If this is in octal form check whether it is palindrome or not. If the number in decimal, then convert it to octal then check whether it is palindrome or not.
So, if the input is like num = 178, then the output will be True as the number is not in octal form (8 is not valid symbol in octal but valid in decimal), then convert it to octal which is 262 and this is palindrome.
To solve this, we will follow these steps −
- base := 8 when all digits of num is below 8, otherwise 10
- oct_list := a new list
- while num is not 0, do
- insert (num mod base) at the end of oct_list
- num := quotient of (num / base)
- j := size of oct_list - 1
- k := 0
- while k <= j, do
- if oct_list[j] is not same as oct_list[k], then
- return False
- j := j - 1, k := k + 1
- if oct_list[j] is not same as oct_list[k], then
- return True
Example
Let us see the following implementation to get better understanding −
def is_all_under_8(num): while num: if (num % 10) >= 8: return False else: num = int(num / 10) return True def solve(num): base = 8 if(is_all_under_8(num) == False) else 10 oct_list = [] while num != 0: oct_list.append(num % base) num = int(num / base) j = len(oct_list)-1 k = 0 while k <= j: if oct_list[j] != oct_list[k]: return False j-=1 k+=1 return True num = 178 print(solve(num))
Input
178
Output
True
- Related Articles
- Recursive program to check if number is palindrome or not in C++
- Python program to check if a string is palindrome or not
- Check if any anagram of a string is palindrome or not in Python
- Check if given number is Emirp Number or not in Python
- Write a C# program to check if a number is Palindrome or not
- Program to check a string is palindrome or not in Python
- Check if a number is an Achilles number or not in Python
- Check if the given number is Ore number or not in Python
- Check if all elements of the array are palindrome or not in Python
- Check if a number is Primorial Prime or not in Python
- Program to check a number is palindrome or not without help of a string in Python
- C++ Program to Check Whether a Number is Palindrome or Not
- Golang Program to check if the binary representation of a number is palindrome or not
- C Program to check if an Array is Palindrome or not
- C# program to check if a string is palindrome or not

Advertisements