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 = 6



Input: arr[] = {56, 455, 546, 234}
Output: 0

Approach: For every element of the array, if it is a one digit number then add 1 to the answer for its digit else check if the number is a palindrome. If yes then find the count of its digits and add it to the answer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to return the reverse of n
int reverse(int n)
{
    int rev = 0;
    while (n > 0) 
    {
        int d = n % 10;
        rev = rev * 10 + d;
        n = n / 10;
    }
    return rev;
}
  
// Function that returns true
// if n is a palindrome
bool isPalin(int n)
{
    return (n == reverse(n));
}
  
// Function to return the
// count of digits of n
int countDigits(int n)
{
    int c = 0;
    while (n > 0) 
    {
        n = n / 10;
        c++;
    }
    return c;
}
  
// Function to return the count of digits
// in all the palindromic numbers of arr[]
int countPalinDigits(int arr[], int n)
{
    int s = 0;
    for (int i = 0; i < n; i++) 
    {
  
        // If arr[i] is a one digit number
        // or it is a palindrome
        if (arr[i] < 10 || isPalin(arr[i])) 
        {
            s += countDigits(arr[i]);
        }
    }
    return s;
}
  
// Driver code
int main()
{
    int arr[] = { 121, 56, 434 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << (countPalinDigits(arr, n));
    return 0;
}
  
// This code is contributed by mits

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Function to return the reverse of n
    static int reverse(int n)
    {
        int rev = 0;
        while (n > 0) {
            int d = n % 10;
            rev = rev * 10 + d;
            n = n / 10;
        }
        return rev;
    }
  
    // Function that returns true
    // if n is a palindrome
    static boolean isPalin(int n)
    {
        return (n == reverse(n));
    }
  
    // Function to return the
    // count of digits of n
    static int countDigits(int n)
    {
        int c = 0;
        while (n > 0) {
            n = n / 10;
            c++;
        }
        return c;
    }
  
    // Function to return the count of digits
    // in all the palindromic numbers of arr[]
    static int countPalinDigits(int[] arr, int n)
    {
        int s = 0;
        for (int i = 0; i < n; i++) {
  
            // If arr[i] is a one digit number
            // or it is a palindrome
            if (arr[i] < 10 || isPalin(arr[i])) {
                s += countDigits(arr[i]);
            }
        }
        return s;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 121, 56, 434 };
        int n = arr.length;
        System.out.println(countPalinDigits(arr, n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the reverse of n 
def reverse(n): 
    rev = 0
    while (n > 0):
        d = n % 10
        rev = rev * 10 + d; 
        n = n // 10
    return rev; 
  
# Function that returns true 
# if n is a palindrome 
def isPalin(n): 
    return (n == reverse(n)); 
  
  
# Function to return the 
# count of digits of n 
def countDigits(n): 
    c = 0
    while (n > 0): 
        n = n // 10
        c += 1;
    return c; 
  
# Function to return the count of digits 
# in all the palindromic numbers of arr[] 
def countPalinDigits(arr, n): 
    s = 0
    for i in range(n):
  
        # If arr[i] is a one digit number 
        # or it is a palindrome 
        if (arr[i] < 10 or isPalin(arr[i])):
            s += countDigits(arr[i]); 
  
    return s; 
  
  
# Driver code 
arr = [ 121, 56, 434 ]; 
n = len(arr); 
print(countPalinDigits(arr, n)); 
  
# This code contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
      
class GFG 
{
  
    // Function to return the reverse of n
    static int reverse(int n)
    {
        int rev = 0;
        while (n > 0)
        {
            int d = n % 10;
            rev = rev * 10 + d;
            n = n / 10;
        }
        return rev;
    }
  
    // Function that returns true
    // if n is a palindrome
    static bool isPalin(int n)
    {
        return (n == reverse(n));
    }
  
    // Function to return the
    // count of digits of n
    static int countDigits(int n)
    {
        int c = 0;
        while (n > 0) 
        {
            n = n / 10;
            c++;
        }
        return c;
    }
  
    // Function to return the count of digits
    // in all the palindromic numbers of arr[]
    static int countPalinDigits(int[] arr, int n)
    {
        int s = 0;
        for (int i = 0; i < n; i++)
        {
  
            // If arr[i] is a one digit number
            // or it is a palindrome
            if (arr[i] < 10 || isPalin(arr[i])) 
            {
                s += countDigits(arr[i]);
            }
        }
        return s;
    }
  
    // Driver code
    public static void Main()
    {
        int[] arr = { 121, 56, 434 };
        int n = arr.Length;
        Console.WriteLine(countPalinDigits(arr, n));
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Output:

6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.