Saturday, September 6, 2025
HomeData Modelling & AICheck if number is palindrome or not in base B

Check if number is palindrome or not in base B

Given an integer N, the task is to check if 

N_B
 

(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 

N_B
 

is equal to N or not. Follow the steps below to solve the problem. 

 

  1. Initialize the variable, rev = 0 to store the reverse of N.
  2. Extract the digits of

N_B

  1. by N % B.
  2. For each digit of

N_B

  1. Update rev= rev * B + N % B
  2. Finally, check if N is equal to rev or not

 

Below is the implementation of the above approach:
 

 

C++




// 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 not
int 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 Code
int main()
{
    int N = 5, B = 2;
    if (checkPalindromeB(N, B)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


Java




// Java program to implement
// the above approach
class GFG{
 
// Function to check if N in
// base B is palindrome or not
static 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 code
public 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




# Python3 program to implement
# the above approach
 
# Function to check if N in
# base B is palindrome or not
def 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 code
if __name__ == '__main__':
    N = 5; B = 2;
 
    if (checkPalindromeB(N, B)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Princi Singh


C#




// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to check if N in
// base B is palindrome or not
static 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 code
public 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


Javascript




<script>
 
// Javascript program to implement
// the above approach
 
// Function to check if N in
// base B is palindrome or not
function 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 Code
var N = 5, B = 2;
if (checkPalindromeB(N, B)) {
    document.write("Yes");
}
else {
    document.write("No");
}
 
 
</script>


Output: 

Yes

 

Time Complexity:O(logBN) 
Auxiliary Space:O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS