Given a number **N**, the task is to find the sum of all N digits palindromic numbers (formed by digits from 1 to 9) that are divisible by 9.

**Examples:**

Input:N = 1

Output:9

Explanation:

Only 9 is a palindromic number of 1 digit divisible by 9

Input:N = 3

Output:4995

Explanation:

Three-digit Palindromic Numbers divisible by 9 are –

171, 252, 333, 414, 585, 666, 747, 828, 999

**Approach:** The key observation in the problem is that if a number is divisible by 9, then the sum of digits of that number is also divisible by 9. Another observation is if we count the number of N-digit palindromic numbers using the digits from 1 to 9, then it can be observed that

Occurrence of each digit = (count of N-digit numbers / 9)

Therefore,

- First find the count of N-digit Palindromic numbers divisible by 9, as:

- Then if N is 1 or 2, the sum will be simply 9 and 99 respectively, as they are the only palindromic numbers of 1 and 2 digits.
- If N > 2, then the sum for Nth digit palindromic numbers divisible by 9 is

- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- N digit numbers divisible by 5 formed from the M digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count numbers formed by given two digit with sum having given digits
- Count of N digit palindromic numbers divisible by 9
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Find Nth even length palindromic number formed using digits X and Y
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Print all n-digit numbers whose sum of digits equals to given sum
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of N-digit numbers having digit XOR as single digit
- Check if the sum of digits of number is divisible by all of its digits
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Sum of all numbers that can be formed with permutations of n digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Smallest N digit number divisible by all possible prime digits
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the sum ` `// of all the N digit palindromic ` `// numbers divisible by 9 ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function for finding count of ` `// N digits palindrome which ` `// are divisible by 9 ` `int` `countPalindrome(` `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; ` `} ` ` ` `// Function for finding sum of N ` `// digits palindrome which are ` `// divisible by 9 ` `int` `sumPalindrome(` `int` `n) ` `{ ` ` ` `// count the possible ` ` ` `// number of palindrome ` ` ` `int` `count = countPalindrome(n); ` ` ` ` ` `int` `res = 0; ` ` ` ` ` `if` `(n == 1) ` ` ` `return` `9; ` ` ` `if` `(n == 2) ` ` ` `return` `99; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `res = res * 10 + count * 5; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `cout << sumPalindrome(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the sum ` `// of all the N digit palindromic ` `// numbers divisible by 9 ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function for finding count of ` `// N digits palindrome which ` `// are divisible by 9 ` `static` `int` `countPalindrome(` `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; ` `} ` ` ` `// Function for finding sum of N ` `// digits palindrome which are ` `// divisible by 9 ` `static` `int` `sumPalindrome(` `int` `n) ` `{ ` ` ` ` ` `// Count the possible ` ` ` `// number of palindrome ` ` ` `int` `count = countPalindrome(n); ` ` ` ` ` `int` `res = ` `0` `; ` ` ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `9` `; ` ` ` `if` `(n == ` `2` `) ` ` ` `return` `99` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `res = res * ` `10` `+ count * ` `5` `; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `System.out.println(sumPalindrome(n)); ` `} ` `} ` ` ` `// This code is contributed by ANKITKUMAR34 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# sum of all the N digit palindromic ` `# numbers divisible by 9 ` ` ` `# Function for finding count of ` `# N digits palindrome which ` `# are divisible by 9 ` `def` `countPalindrome(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 ` ` ` `# Function for finding sum of N ` `# digits palindrome which are ` `# divisible by 9 ` `def` `sumPalindrome(n): ` ` ` ` ` `# Count the possible ` ` ` `# number of palindrome ` ` ` `count ` `=` `countPalindrome(n) ` ` ` ` ` `res ` `=` `0` ` ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `9` ` ` `if` `(n ` `=` `=` `2` `): ` ` ` `return` `99` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `res ` `=` `res ` `*` `10` `+` `count ` `*` `5` ` ` ` ` `return` `res ` ` ` `# Driver Code ` `n ` `=` `3` ` ` `print` `(sumPalindrome(n)) ` ` ` `# This code is contributed by ANKITKUMAR34 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the sum ` `// of all the N digit palindromic ` `// numbers divisible by 9 ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function for finding count of ` `// N digits palindrome which ` `// are divisible by 9 ` `static` `int` `countPalindrome(` `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; ` `} ` ` ` `// Function for finding sum of N ` `// digits palindrome which are ` `// divisible by 9 ` `static` `int` `sumPalindrome(` `int` `n) ` `{ ` ` ` ` ` `// Count the possible ` ` ` `// number of palindrome ` ` ` `int` `count = countPalindrome(n); ` ` ` ` ` `int` `res = 0; ` ` ` ` ` `if` `(n == 1) ` ` ` `return` `9; ` ` ` `if` `(n == 2) ` ` ` `return` `99; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `res = res * 10 + count * 5; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `Console.WriteLine(sumPalindrome(n)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

4995

## Recommended Posts:

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.