Check if number is palindrome or not in Octal | C ++ program to check if octal representation of a number is prime ; Function to Check no is in octal or not ; Function To check no is palindrome or not ; If number is already in octal , we traverse digits using repeated division with 10. Else we traverse digits using repeated division with 8 ; To store individual digits ; Traversing all digits ; checking if octal no is palindrome ; Driver code
#include <iostream> NEW_LINE using namespace std ; const int MAX_DIGITS = 20 ; bool isOctal ( long n ) { while ( n ) { if ( ( n % 10 ) >= 8 ) return false ; else n = n / 10 ; } return true ; } int isPalindrome ( long n ) { int divide = ( isOctal ( n ) == false ) ? 8 : 10 ; int octal [ MAX_DIGITS ] ; int i = 0 ; while ( n != 0 ) { octal [ i ++ ] = n % divide ; n = n / divide ; } for ( int j = i - 1 , k = 0 ; k <= j ; j -- , k ++ ) if ( octal [ j ] != octal [ k ] ) return false ; return true ; } int main ( ) { long n = 97 ; if ( isPalindrome ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }