Count rotations divisible by 8 | Function to count of all rotations divisible by 8 ; For single digit number ; For two - digit numbers ( considering all pairs ) ; first pair ; second pair ; Considering all three - digit sequences ; Considering the number formed by the last digit and the first two digits ; Considering the number formed by the last two digits and the first digit ; Required count of rotations ; Driver Code
function countRotationsDivBy8 ( n ) { let len = n . length ; let count = 0 ; if ( len == 1 ) { let oneDigit = n [ 0 ] - ' ' ; if ( oneDigit % 8 == 0 ) return 1 ; return 0 ; } if ( len == 2 ) { let first = ( n [ 0 ] - ' ' ) * 10 + ( n [ 1 ] - ' ' ) ; let second = ( n [ 1 ] - ' ' ) * 10 + ( n [ 0 ] - ' ' ) ; if ( first % 8 == 0 ) count ++ ; if ( second % 8 == 0 ) count ++ ; return count ; } let threeDigit ; for ( let i = 0 ; i < ( len - 2 ) ; i ++ ) { threeDigit = ( n [ i ] - ' ' ) * 100 + ( n [ i + 1 ] - ' ' ) * 10 + ( n [ i + 2 ] - ' ' ) ; if ( threeDigit % 8 == 0 ) count ++ ; } threeDigit = ( n [ len - 1 ] - ' ' ) * 100 + ( n [ 0 ] - ' ' ) * 10 + ( n [ 1 ] - ' ' ) ; if ( threeDigit % 8 == 0 ) count ++ ; threeDigit = ( n [ len - 2 ] - ' ' ) * 100 + ( n [ len - 1 ] - ' ' ) * 10 + ( n [ 0 ] - ' ' ) ; if ( threeDigit % 8 == 0 ) count ++ ; return count ; } let n = " " ; document . write ( " " + countRotationsDivBy8 ( n ) ) ;