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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.