Thursday, October 16, 2025
HomeData Modelling & AICount of N-digit Palindrome numbers

Count of N-digit Palindrome numbers

Given an integer N, the task is to find the count of N-digit Palindrome numbers.
Examples: 
 

Input: N = 1 
Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9} are all the possible 
single digit palindrome numbers.
Input: N = 2 
Output:
 

 

Approach: The first digit can be any of the 9 digits (not 0) and the last digit will have to be same as the first in order for it to be palindrome, the second and the second last digit can be any of the 10 digits and same goes for the rest of the digits. So, for any value of N, the count of N-digit palindromes will be 9 * 10(N – 1) / 2.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count
// of N-digit palindrome numbers
int nDigitPalindromes(int n)
{
    return (9 * pow(10, (n - 1) / 2));
}
 
// Driver code
int main()
{
    int n = 2;
 
    cout << nDigitPalindromes(n);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
     
// Function to return the count
// of N-digit palindrome numbers
static int nDigitPalindromes(int n)
{
    return (9 * (int)Math.pow(10,
           (n - 1) / 2));
}
 
// Driver code
public static void main(String []args)
{
    int n = 2;
 
    System.out.println(nDigitPalindromes(n));
}
}
 
// This code is contributed by Code_Mech


Python3




# Python3 implementation of the approach
 
# Function to return the count
# of N-digit palindrome numbers
def nDigitPalindromes(n) :
 
    return (9 * pow(10, (n - 1) // 2));
 
# Driver code
if __name__ == "__main__" :
 
    n = 2;
 
    print(nDigitPalindromes(n));
 
# This code is contributed by AnkitRai01


C#




// C# implementation of the approach
using System;
     
class GFG
{
     
// Function to return the count
// of N-digit palindrome numbers
static int nDigitPalindromes(int n)
{
    return (9 * (int)Math.Pow(10,
           (n - 1) / 2));
}
 
// Driver code
public static void Main(String []args)
{
    int n = 2;
 
    Console.WriteLine(nDigitPalindromes(n));
}
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the count
// of N-digit palindrome numbers
function nDigitPalindromes(n)
{
    return (9 * Math.pow(10, parseInt((n - 1) / 2)));
}
 
// Driver code
var n = 2;
document.write(nDigitPalindromes(n));
 
</script>


Output: 

9

 

Time Complexity: O(log n)

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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS