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
using System ; class GFG { static bool isPalindrome ( string s ) { int left = 0 , right = s . Length - 1 ; while ( left <= right ) { if ( s [ left ] != s [ right ] ) { return false ; } left ++ ; right -- ; } return true ; } static long getSum ( int n ) { int start = ( int ) Math . Pow ( 10 , n - 1 ) ; int end = ( int ) ( Math . Pow ( 10 , n ) - 1 ) ; long sum = 0 ; for ( int i = start ; i <= end ; i ++ ) { string s = i . ToString ( ) ; ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } public static void Main ( ) { int n = 1 ; long ans = getSum ( n ) ; Console . Write ( ans ) ; } }