Check if a large number is divisible by 13 or not | Returns true if number is divisible by 13 else returns false ; Append required 0 s at the beginning . ; Same as strcat ( num , "00" ) ; in c . ; Same as strcat ( num , "0" ) ; in c . ; Alternatively add / subtract digits in group of three to result . ; Store group of three numbers in group variable . ; Generate alternate series of plus and minus ; Driver code
function checkDivisibility ( num ) { let length = num . length ; if ( length == 1 && num [ 0 ] == ' ' ) return true ; if ( length % 3 == 1 ) { num += " " ; length += 2 ; } else if ( length % 3 == 2 ) { num += " " ; length += 1 ; } let sum = 0 ; p = 1 ; for ( let i = length - 1 ; i >= 0 ; i -- ) { group = 0 ; group += num [ i -- ] - ' ' ; group += ( num [ i -- ] - ' ' ) * 10 ; group += ( num [ i ] - ' ' ) * 100 ; sum = sum + group * p ; p *= ( - 1 ) ; } sum = Math . abs ( sum ) ; return ( sum % 13 == 0 ) ; } let number = " " ; if ( checkDivisibility ( number ) ) document . write ( number + " " ) ; else document . write ( number + " " ) ;