Sum of all N digit palindrome numbers | C ++ program for the above approach ; Function to check palindrome ; Function to calculate the sum of n - digit palindrome ; Run a loop to check all possible palindrome ; If palindrome append sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string & s ) { int left = 0 , right = s . size ( ) - 1 ; while ( left <= right ) { if ( s [ left ] != s [ right ] ) { return false ; } left ++ ; right -- ; } return true ; } long long getSum ( int n ) { int start = pow ( 10 , n - 1 ) ; int end = pow ( 10 , n ) - 1 ; long long sum = 0 ; for ( int i = start ; i <= end ; i ++ ) { string s = to_string ( i ) ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } int main ( ) { int n = 1 ; long long ans = getSum ( n ) ; cout << ans << ' ' ; return 0 ; }