Check if number is palindrome or not in base B - GeeksforGeeks

17 Mar, 2021
Given an integer N, the task is to check if 
 
(N in base B) is palindrome or not.
 
Examples: 
 
Input: N = 5, B = 2 Output: Yes Explanation: (5)10 = (101)2 which is palindrome. Therefore, the required output is Yes.Input: N = 4, B = 2 Output: No  
 
Approach: The problem can be solved by checking if the decimal value of the reverse of 
 
is equal to N or not. Follow the steps below to solve the problem. 
 
Initialize the variable, rev = 0 to store the reverse of N.Extract the digits of
Initialize the variable, rev = 0 to store the reverse of N.
Extract the digits of
by N % B.For each digit of
by N % B.
For each digit of
Update rev= rev * B + N % BFinally, check if N is equal to rev or not
Update rev= rev * B + N % B
Finally, check if N is equal to rev or not
 
Below is the implementation of the above approach: 
 
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to check if N in// base B is palindrome or notint checkPalindromeB(int N, int B){    // Stores the reverse of N    int rev = 0;     // Stores the value of N    int N1 = N;     // Extract all the digits of N    while (N1) {        // Generate its reverse        rev = rev * B + N1 % B;        N1 = N1 / B;    }     return N == rev;} // Driver Codeint main(){    int N = 5, B = 2;    if (checkPalindromeB(N, B)) {        cout << "Yes";    }    else {        cout << "No";    }}
// Java program to implement// the above approachclass GFG{ // Function to check if N in// base B is palindrome or notstatic boolean checkPalindromeB(int N,                                int B){         // Stores the reverse of N    int rev = 0;     // Stores the value of N    int N1 = N;     // Extract all the digits of N    while (N1 > 0)    {                 // Generate its reverse        rev = rev * B + N1 % B;        N1 = N1 / B;    }    return N == rev;} // Driver codepublic static void main(String[] args){    int N = 5, B = 2;         if (checkPalindromeB(N, B))    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by Dewanti
# Python3 program to implement# the above approach # Function to check if N in# base B is palindrome or notdef checkPalindromeB(N, B):     # Stores the reverse of N    rev = 0;     # Stores the value of N    N1 = N;     # Extract all the digits of N    while (N1 > 0):         # Generate its reverse        rev = rev * B + N1 % B;        N1 = N1 // B;         return N == rev; # Driver codeif __name__ == '__main__':    N = 5; B = 2;     if (checkPalindromeB(N, B)):        print("Yes");    else:        print("No"); # This code is contributed by Princi Singh
// C# program to implement// the above approachusing System; class GFG{ // Function to check if N in// base B is palindrome or notstatic bool checkPalindromeB(int N,                             int B){         // Stores the reverse of N    int rev = 0;     // Stores the value of N    int N1 = N;     // Extract all the digits of N    while (N1 > 0)    {                 // Generate its reverse        rev = rev * B + N1 % B;        N1 = N1 / B;    }    return N == rev;} // Driver codepublic static void Main(String[] args){    int N = 5, B = 2;         if (checkPalindromeB(N, B))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by Amit Katiyar
<script> // Javascript program to implement// the above approach // Function to check if N in// base B is palindrome or notfunction checkPalindromeB(N, B){    // Stores the reverse of N    var rev = 0;     // Stores the value of N    var N1 = N;     // Extract all the digits of N    while (N1) {        // Generate its reverse        rev = rev * B + N1 % B;        N1 = parseInt(N1 / B);    }     return N == rev;} // Driver Codevar N = 5, B = 2;if (checkPalindromeB(N, B)) {    document.write("Yes");}else {    document.write("No");}  </script>
Yes
 
Time Complexity:O(logBN) Auxiliary Space:O(1)
dewantipandeydp
amit143katiyar
princi singh
noob2000
palindrome
Reverse
Mathematical
Mathematical
palindrome
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python
Count ways to reach the n'th stair