Count of N digit palindromic numbers divisible by 9
Last Updated :
05 Dec, 2022
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 segregated 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;
}
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
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
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
JavaScript
<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>
Time Complexity: O(log9n)
Auxiliary Space: O(1)
Similar Reads
Count of N-digit Palindrome numbers Given an integer N, the task is to find the count of N-digit Palindrome numbers.Examples: Input: N = 1 Output: 9 {1, 2, 3, 4, 5, 6, 7, 8, 9} are all the possible single digit palindrome numbers.Input: N = 2 Output: 9 Approach: The first digit can be any of the 9 digits (not 0) and the last digit wil
2 min read
Count N digits numbers with sum divisible by K Given two integers N and K, the task is to count all N digits numbers whose sum of digits is divisible by K. Examples: Input: N = 2, K = 7Output: 12Explanation: 2 digits numbers with sum divisible by 7 are: {16, 25, 34, 43, 52, 59, 61, 68, 70, 77, 86, 95}.Therefore, the required output is 12. Input:
15+ min read
Count n digit numbers divisible by given number Given number of digit n and a number, the task is to count all the numbers which are divisible by that number and having n digit. Examples : Input : n = 2, number = 7Output : 13Explanation: There are nine n digit numbers that are divisible by 7. Numbers are 14, 21, 28, 35, 42, 49, .... 91, 98 Input
8 min read
Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9 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 Palindro
6 min read
Count the number of digits of palindrome numbers in an array Given an array arr[] with N integers. The task is to count all the digits of all palindrome numbers present in the array.Examples: Input: arr[] = {121, 56, 434} Output: 6 Only 121 and 434 are palindromes and digitCount(121) + digitCount(434) = 3 + 3 = 6Input: arr[] = {56, 455, 546, 234} Output: 0 Ap
8 min read