Sum of all palindrome numbers present in an Array

04 Jun, 2022
Given an array arr[] of N positive integers. The task is to find the sum of all palindrome numbers present in the array. Print the total sum.
A palindrome number is a number which when reversed is equal to the initial number. Example: 121 is palindrome(reverse(121) = 121), 123 is not palindrome(reverse(123) = 321). 
Note: Consider palindrome numbers of length greater than 1 while calculating the sum.
Examples: 
Input : arr[] ={12, 313, 11, 44, 9, 1} 
Output : 368

Input : arr[] = {12, 11, 121}
Output : 132
Approach: The idea is to implement a reverse function that reverses a number from the right to left. Implement a function that checks for palindrome numbers and finally traverse the array and calculate sum of all elements which are palindrome.
Below is the implementation of the above approach: 
C++
Java
C#
Python3
Javascript
// C++ program to calculate the sum of all// palindromic numbers in array#include<bits/stdc++.h>using namespace std; // Function to reverse a number nint reverse(int n){    int d = 0, s = 0;     while (n > 0)    {        d = n % 10;        s = s * 10 + d;        n = n / 10;    }     return s;} // Function to check if a number n is// palindromebool isPalin(int n){    // If n is equal to the reverse of n    // it is a palindrome    return n == reverse(n);} // Function to calculate sum of all array// elements which are palindromeint sumOfArray(int arr[], int n){    int s = 0;     for (int i = 0; i < n; i++)    {        if ((arr[i] > 10) && isPalin(arr[i]))        {             // summation of all palindrome numbers            // present in array            s += arr[i];        }    }    return s;} // Driver Codeint main(){    int n = 6;     int arr[] = { 12, 313, 11, 44, 9, 1 };     cout << sumOfArray(arr, n);    return 0;} // This code is contributed by mits
// Java program to calculate the sum of all// palindromic numbers in array class GFG {     // Function to reverse a number n    static int reverse(int n)    {        int d = 0, s = 0;         while (n > 0) {            d = n % 10;            s = s * 10 + d;            n = n / 10;        }         return s;    }     // Function to check if a number n is    // palindrome    static boolean isPalin(int n)    {        // If n is equal to the reverse of n        // it is a palindrome        return n == reverse(n);    }     // Function to calculate sum of all array    // elements which are palindrome    static int sumOfArray(int[] arr, int n)    {        int s = 0;         for (int i = 0; i < n; i++) {            if ((arr[i] > 10) && isPalin(arr[i])) {                 // summation of all palindrome numbers                // present in array                s += arr[i];            }        }         return s;    }     // Driver Code    public static void main(String[] args)    {        int n = 6;         int[] arr = { 12, 313, 11, 44, 9, 1 };         System.out.println(sumOfArray(arr, n));    }}
// C# program to calculate the sum of all// palindromic numbers in arrayusing System; class GFG{     // Function to reverse a number n    static int reverse(int n)    {        int d = 0, s = 0;         while (n > 0)        {            d = n % 10;            s = s * 10 + d;            n = n / 10;        }         return s;    }     // Function to check if a number n is    // palindrome    static bool isPalin(int n)    {        // If n is equal to the reverse of n        // it is a palindrome        return n == reverse(n);    }     // Function to calculate sum of all array    // elements which are palindrome    static int sumOfArray(int[] arr, int n)    {        int s = 0;         for (int i = 0; i < n; i++)        {            if ((arr[i] > 10) && isPalin(arr[i]))            {                 // summation of all palindrome numbers                // present in array                s += arr[i];            }        }         return s;    }     // Driver Code    public static void Main(String[] args)    {        int n = 6;         int[] arr = { 12, 313, 11, 44, 9, 1 };         Console.WriteLine(sumOfArray(arr, n));    }} /* This code contributed by PrinciRaj1992 */
# Python3 program to calculate the sum of all# palindromic numbers in array # Function to reverse a number ndef reverse(n) :         d = 0; s = 0;     while (n > 0) :         d = n % 10;        s = s * 10 + d;        n = n // 10;     return s;      # Function to check if a number n is# palindromedef isPalin(n) :     # If n is equal to the reverse of n    # it is a palindrome    return n == reverse(n);  # Function to calculate sum of all array# elements which are palindromedef sumOfArray(arr, n) :    s = 0;         for i in range(n) :        if ((arr[i] > 10) and isPalin(arr[i])) :                     # summation of all palindrome numbers            # present in array            s += arr[i];                 return s;   # Driver Codeif __name__ == "__main__" :     n = 6;     arr = [ 12, 313, 11, 44, 9, 1 ];     print(sumOfArray(arr, n));        # This code is contributed by AnkitRai01
<script> // Javascript program to calculate the// sum of all palindromic numbers in array // Function to reverse a number nfunction reverse( n){    let d = 0, s = 0;     while (n > 0)    {        d = n % 10;        s = s * 10 + d;        n = Math.floor(n / 10);    }    return s;} // Function to check if a number n is// palindromefunction isPalin(n){         // If n is equal to the reverse of n    // it is a palindrome    return n == reverse(n);} // Function to calculate sum of all array// elements which are palindromefunction sumOfArray( arr, n){    let s = 0;     for(let i = 0; i < n; i++)    {        if ((arr[i] > 10) && isPalin(arr[i]))        {                         // Summation of all palindrome            // numbers present in array            s += arr[i];        }    }    return s;} // Driver Codelet n = 6;let arr = [ 12, 313, 11, 44, 9, 1 ]; document.write(sumOfArray(arr, n)); // This code is contributed by jana_sayantan </script>
368
Time Complexity: O(n * max(arr)), where max(arr) is the largest element of the array arr.
Auxiliary Space: O(1), since no extra space has been taken.
Another approach :
In java, we can easily implement it by using StringBuilder object and the reverse() method. 
Step 1: Get the input from the user
Step 2: Initialize sum=0 and iterate through each element.
Step 3: Now, convert the integer element to string by using Integer.toString(array[i])
Step 4:Reverse it by StringBuilder object using reverse() method and toString() is used to convert the object to string.
Step 5: Equalize both the string values and check element greater than 9. If it satisfies the condition, sum the elements.
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG {    public static void main (String[] args) {      int array[]={12, 313, 11, 44, 9, 1};      int n=array.length;      int sum=0;      for(int i=0;i<n;i++){        String str=Integer.toString(array[i]);        String rev=new StringBuilder(str).reverse().toString();        if(str.equals(rev) && array[i]>9){          sum=sum+array[i];        }      }      System.out.println(sum);    }}
368

In python, we can execute it by implementing the below approach.
Step 1 : Initialize the list or array and sum=0.
Step 2 : Iterate the list using for loop and convert the integer element to string using str().
Step 3 : Reverse the string using string_element[ : : -1].
Step 4 :Equalize both the string values and check element greater than 9. If it satisfies the condition, sum the elements.
Python3
# code lis=[12, 313, 11, 44, 9, 1]sum=0;for i in lis:  string_conversion=str(i)  rev_string=string_conversion[ : : -1]  if(string_conversion==rev_string and i>9):    sum=sum+iprint(sum)     
368

Time Complexity : O(n)
princiraj1992
Mithun Kumar
ankthon
jana_sayantan
rishav1329
keerthikarathan123
palindrome
Arrays
Mathematical
School Programming
Arrays
Mathematical
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.