# Count of all N digit numbers such that num + Rev(num) = 10^N – 1

Given an integer **N**, the task is to find the count of all **N** digit numbers such that **num + Rev(num) = 10 ^{N} – 1**

**Examples:**

Input:N = 2

Output:9

All possible numbers are

18 + 81 = 99

27 + 72 = 99

36 + 45 = 99

45 + 54 = 99

54 + 45 = 99

63 + 54 = 99

72 + 27 = 99

81 + 18 = 99

90 + 09 = 99

Input:N = 4

Output:90

**Approach** There are 2 cases:

If **n is odd** then answer will be 0.

Let

n = 3thennum = d1d2d3andrev(num) = d3d2d1

num + rev(num) should be 999 since n = 3.

So the below equations must be satisfied

d1 + d3 = 9 … (1)

d2 + d2 = 9 … (2)

d3 + d1 = 9 … (3)

Considering equation 2:

d2 + d2 = 9

2 * d2 = 9

d2 = 4.5 which is not possible because the digit of a number should always be a whole number.

Therefore If n is odd then answer will be 0.

If **n is even** then answer will be **9 * 10 ^{(N / 2 – 1)}**.

Let

n = 4thennum = d1d2d3d4andrev(num) = d4d3d2d1

So the below equations should be satisfied

d1 + d4 = 9 … (1)

d2 + d3 = 9 … (2)

d3 + d2 = 9 … (3)

d4 + d1 = 9 … (4)Considering equation 1:

d1 + d4 = 9. It can be true in 9 ways:

(1 + 8), (2 + 7), (3 + 6), (4 + 5), (5 + 4), (6 + 3), (7 + 2), (8 + 1) and (9 + 0)

Similarly other equations will also have 9 solutions + 1 more solution since the remaining digits are not the first and last digit of the number and we can take sum of the form(0 + 9).

And since half of the equations are same

Therefore, if n is even then answer will be9 * 10.^{(N / 2 – 1)}

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 such numbers ` `int` `countNumbers(` `int` `n) ` `{ ` ` ` ` ` `// If n is odd ` ` ` `if` `(n % 2 == 1) ` ` ` `return` `0; ` ` ` ` ` `return` `(9 * ` `pow` `(10, n / 2 - 1)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 2; ` ` ` `cout << countNumbers(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the ` ` ` `// count of such numbers ` ` ` `static` `int` `countNumbers(` `int` `n) ` ` ` `{ ` ` ` ` ` `// If n is odd ` ` ` `if` `(n % ` `2` `== ` `1` `) ` ` ` `return` `0` `; ` ` ` ` ` `return` `(` `9` `* (` `int` `)Math.pow(` `10` `, n / ` `2` `- ` `1` `)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `2` `; ` ` ` `System.out.print(countNumbers(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the ` `# count of such numbers ` `def` `countNumbers(n): ` ` ` ` ` `# If n is odd ` ` ` `if` `n ` `%` `2` `=` `=` `1` `: ` ` ` `return` `0` ` ` ` ` `return` `(` `9` `*` `pow` `(` `10` `, n ` `/` `/` `2` `-` `1` `)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `2` ` ` `print` `(countNumbers(n)) ` ` ` `# This code is contributed ` `# by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to return the ` ` ` `// count of such numbers ` ` ` `static` `int` `countNumbers(` `int` `n) ` ` ` `{ ` ` ` ` ` `// If n is odd ` ` ` `if` `(n % 2 == 1) ` ` ` `return` `0; ` ` ` ` ` `return` `(9 * (` `int` `)Math.Pow(10, n / 2 - 1)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 2; ` ` ` `Console.WriteLine(countNumbers(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

9

**Time Complexity:** O(1)

## Recommended Posts:

- Count of Numbers in Range where first digit is equal to last digit of the number
- Count n digit numbers not having a particular digit
- Count numbers having 0 as a digit
- Count numbers having 0 as a digit
- Count numbers from 1 to n that have 4 as a digit
- Count all possible N digit numbers that satisfy the given condition
- Count numbers with unit digit k in given range
- Count numbers formed by given two digit with sum having given digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count n digit numbers divisible by given number
- Count of N digit numbers possible which satisfy the given conditions
- Count of Binary Digit numbers smaller than N
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count numbers with difference between number and its digit sum greater than specific value

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.