Check if the sum of digits of N is palindrome | Function to return the sum of digits of n ; Function that returns true if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digit not same return false ; Removing the leading and trailing digit from number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function that returns true if the digit sum of n is palindrome ; Sum of the digits of n ; If the digit sum is palindrome ; Driver code
function digitSum ( n ) { let sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n = parseInt ( n / 10 , 10 ) ; } return sum ; } function isPalindrome ( n ) { let divisor = 1 ; while ( parseInt ( n / divisor , 10 ) >= 10 ) divisor *= 10 ; while ( n != 0 ) { let leading = parseInt ( n / divisor , 10 ) ; let trailing = n % 10 ; if ( leading != trailing ) return false ; n = parseInt ( ( n % divisor ) / 10 , 10 ) ; divisor = parseInt ( divisor / 100 , 10 ) ; } return true ; } function isDigitSumPalindrome ( n ) { let sum = digitSum ( n ) ; if ( isPalindrome ( sum ) ) return true ; return false ; } let n = 56 ; if ( isDigitSumPalindrome ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;