Related Articles

# Probability that a N digit number is palindrome

• Difficulty Level : Medium
• Last Updated : 08 Jun, 2021

Given an integer N, the task is to find the probability that a number with a number of digits as N is a palindrome.
The number may have leading zeros.
Examples:

Input: N = 5
Output: 1 / 100
Input: N = 6
Output: 1 / 1000

Solution:

• As leading zeroes are allowed total number of N digit number is 10N.
• A number is a palindrome when first N/2 digits match with last N/2 digits in reverse order.
• For even number of digits, we can pick first N/2 digits and then duplicate them to form the rest of N/2 digits so we can choose (N)/2 digits.
• For an odd number of digits we can pick first (N-1)/2 digits and then duplicate them to form the rest of (N-1)/2 digits so we can choose (N+1)/2 digits.
• So the probability that an N digit number is palindrome is 10ceil( N / 2 ) / 10N or 1 / 10floor( N / 2 )

Below is the implementation of the approach:

## C++

 `// C++ code of above approach``#include ``using` `namespace` `std;` `// Find the probability that a``// n digit number is palindrome``void` `solve(``int` `n)``{``    ``int` `n_2 = n / 2;` `    ``// Denominator``    ``string den;``    ``den = ``"1"``;` `    ``// Assign 10^(floor(n/2)) to``    ``// denominator``    ``while` `(n_2--)``        ``den += ``'0'``;` `    ``// Display the answer``    ``cout << 1 << ``"/"` `<< den << ``"\n"``;``}` `// Driver code``int` `main()``{` `    ``int` `N = 5;` `    ``solve(N);` `    ``return` `0;``}`

## Java

 `// Java code of above approach``import` `java.util.*;` `class` `GFG``{` `// Find the probability that a``// n digit number is palindrome``static` `void` `solve(``int` `n)``{``    ``int` `n_2 = n / ``2``;` `    ``// Denominator``    ``String den;``    ``den = ``"1"``;` `    ``// Assign 10^(floor(n/2)) to``    ``// denominator``    ``while` `(n_2-- > ``0``)``        ``den += ``'0'``;` `    ``// Display the answer``    ``System.out.println(``1` `+ ``"/"` `+ den);``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``5``;` `    ``solve(N);``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 code of above approach` `# Find the probability that a``# n digit number is palindrome``def` `solve(n) :` `    ``n_2 ``=` `n ``/``/` `2``;` `    ``# Denominator``    ``den ``=` `"1"``;` `    ``# Assign 10^(floor(n/2)) to``    ``# denominator``    ``while` `(n_2) :``        ``den ``+``=` `'0'``;``        ` `        ``n_2 ``-``=` `1``        ` `    ``# Display the answer``    ``print``(``str``(``1``) ``+` `"/"` `+` `str``(den))``    ` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``N ``=` `5``;` `    ``solve(N);` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{` `// Find the probability that a``// n digit number is palindrome``static` `void` `solve(``int` `n)``{``    ``int` `n_2 = n / 2;` `    ``// Denominator``    ``String den;``    ``den = ``"1"``;` `    ``// Assign 10^(floor(n/2)) to``    ``// denominator``    ``while` `(n_2-- > 0)``        ``den += ``'0'``;` `    ``// Display the answer``    ``Console.WriteLine(1 + ``"/"` `+ den);``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 5;` `    ``solve(N);``}``}` `// This code is contributed by PrinciRaj1992`

## Javascript

 ``
Output:
`1/100`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up