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:1

Explanation:

Only 9 is 1 digit number which is palindrome and divisible by 9.

Input:N = 3

Output:9

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 segreated on the basis of its parity.

**If N is odd:**We can put any number from 1 to 9 in position 1 to (N-1)/2, Similarly, the other digits are chosen in reverse order to form palindromic number and the middle element is chosen on to form the sum of digits divisible by 9. Therefore, there are 9 choices for each position of (N-1)/2 digits of the number due to which the count of such number will be:Count of N-digit Palindromic numbers = 9

^{(N-1)/2}**If N is even:**We can put any number from 1 to 9 at the position from 1 to (N-2)/2, Similarly, the other digits are chosen in reverse order to form palindromic number and the middle element is chosen on to form the sum of digits divisible by 9. Therefore, there are 9 choices for each position of (N-2)/2 digits of the number due to which the count of such number will be:Count of N-digit Palindromic numbers = 9

^{(N-2)/2}

## C++

`// 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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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 ` |

*chevron_right*

*filter_none*

## Python3

`# 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 ` |

*chevron_right*

*filter_none*

## C#

`// 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 ` |

*chevron_right*

*filter_none*

**Output:**

9

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of N-digit numbers having digit XOR as single digit
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count n digit numbers not having a particular digit
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count n digit numbers divisible by given number
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count non-palindromic array elements having same first and last digit
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Count 'd' digit positive integers with 0 as a digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Smallest n digit number divisible by given three numbers
- Sum of n digit numbers divisible by a given number
- N digit numbers divisible by 5 formed from the M digits
- Largest N digit number divisible by given three numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.