Sum of all N digit palindrome numbers | Java 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
import java . util . * ; class GFG { static boolean isPalindrome ( String s ) { int left = 0 , right = s . length ( ) - 1 ; while ( left <= right ) { if ( s . charAt ( left ) != s . charAt ( 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 = String . valueOf ( i ) ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } public static void main ( String [ ] args ) { int n = 1 ; long ans = getSum ( n ) ; System . out . print ( ans ) ; } }