Open In App

Count of N digit palindromic numbers divisible by 9

Given an integer N, the task is to count the number of N digit palindromic numbers containing digits from 1 to 9 and divisible by 9. 
Examples: 
 

Input: N = 1 
Output:
Explanation: 
Only 9 is 1 digit number which is palindrome and divisible by 9.
Input: N = 3 
Output:
Explanation: 
Three digit numbers those are palindrome and divisible by 9 are – 
{171, 252, 333, 414, 585, 666, 747, 828, 999} 
 



 

Approach: The key observation in the problem is if the number is divisible by 9 then sum of digits of the number is also divisible by 9. Therefore, the problem can be segregated on the basis of its parity.
 



Count of N-digit Palindromic numbers =
                 9(N-1)/2
Count of N-digit Palindromic numbers =
                 9(N-2)/2

 




// C++ implementation to count the
// number of N digit palindromic
// numbers divisible by 9
 
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to find the count of
// N digits palindromic numbers
// which are divisible by 9
int countPalindromic(int n)
{
    int count;
 
    // if N is odd
    if (n % 2 == 1) {
        count = pow(9, (n - 1) / 2);
    }
    // if N is even
    else
    {
        count = pow(9, (n - 2) / 2);
    }
    return count;
}
 
// Driver Code
int main()
{
    int n = 3;
    cout << countPalindromic(n);
    return 0;
}




// Java implementation to count the
// number of N digit palindromic
// numbers divisible by 9
import java.util.*;
 
class GFG{
     
// Function to find the count of
// N digits palindromic numbers
// which are divisible by 9
static int countPalindromic(int n)
{
    int count;
 
    // If N is odd
    if (n % 2 == 1)
    {
        count = (int) Math.pow(9, (n - 1) / 2);
    }
     
    // If N is even
    else
    {
        count = (int) Math.pow(9, (n - 2) / 2);
    }
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3;
    System.out.println(countPalindromic(n));
}
}
 
// This code is contributed by ANKITKUMAR34




# Python3 implementation to count the
# number of N digit palindromic
# numbers divisible by 9
 
# Function to find the count of
# N digits palindromic numbers
# which are divisible by 9
def countPalindromic(n):
     
    count = 0
     
    # If N is odd
    if (n % 2 == 1):
        count = pow(9, (n - 1) // 2)
         
    # If N is even
    else:
        count = pow(9, (n - 2) // 2)
         
    return count
 
# Driver Code
n = 3
print(countPalindromic(n))
 
# This code is contributed by ANKITKUMAR34




// C# implementation to count the
// number of N digit palindromic
// numbers divisible by 9
using System;
class GFG{
     
// Function to find the count of
// N digits palindromic numbers
// which are divisible by 9
static int countPalindromic(int n)
{
    int count;
 
    // If N is odd
    if (n % 2 == 1)
    {
        count = (int) Math.Pow(9, (n - 1) / 2);
    }
     
    // If N is even
    else
    {
        count = (int) Math.Pow(9, (n - 2) / 2);
    }
    return count;
}
 
// Driver Code
public static void Main()
{
    int n = 3;
    Console.Write(countPalindromic(n));
}
}
 
// This code is contributed by Nidhi_biet




<script>
 
// Javascript implementation to count the
// number of N digit palindromic
// numbers divisible by 9
 
// Function to find the count of
// N digits palindromic numbers
// which are divisible by 9
function countPalindromic(n)
{
    var count;
 
    // if N is odd
    if (n % 2 == 1) {
        count = Math.pow(9, (n - 1) / 2);
    }
    // if N is even
    else
    {
        count = Math.pow(9, (n - 2) / 2);
    }
    return count;
}
 
// Driver Code
var n = 3;
document.write( countPalindromic(n));
 
</script>

Output: 
9

 

Time Complexity: O(log9n)

Auxiliary Space: O(1)


Article Tags :