Sum of all palindrome numbers present in an Array
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++
// C++ program to calculate the sum of all // palindromic numbers in array #include<bits/stdc++.h> using namespace std; // Function to reverse a number n 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 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 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 int 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
// 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#
// C# program to calculate the sum of all // palindromic numbers in array using 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
# Python3 program to calculate the sum of all # palindromic numbers in array # Function to reverse a number n def 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 # palindrome def 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 palindrome def 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 Code if __name__ == "__main__" : n = 6; arr = [ 12, 313, 11, 44, 9, 1 ]; print(sumOfArray(arr, n)); # This code is contributed by AnkitRai01 |
368
Recommended Posts:
- Sum of all mersenne numbers present in an array
- Sum of all perfect numbers present in an array
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Maximum consecutive numbers present in an array
- Count the number of digits of palindrome numbers in an array
- Sum of all the numbers present at given level in Pascal's triangle
- Sum of all the numbers present at given level in Modified Pascal’s triangle
- First string from the given array whose reverse is also present in the same array
- Probability of a key K present in array
- Sum of first K even-length Palindrome numbers
- Count elements present in first array but not in second
- Find elements which are present in first array and not in second
- Count of N-digit Palindrome numbers
- Find all palindrome numbers of given digits
- Choose two elements from the given array such that their sum is not present in any of the arrays
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.

