Count rotations divisible by 4 | Returns count of all rotations divisible by 4 ; For single digit number ; At - least 2 digit number ( considering all pairs ) ; Considering the number formed by the pair of last digit and 1 st digit ; Driver Code
< ? php function countRotations ( $ n ) { $ len = strlen ( $ n ) ; if ( $ len == 1 ) { $ oneDigit = $ n [ 0 ] - '0' ; if ( $ oneDigit % 4 == 0 ) return 1 ; return 0 ; } $ twoDigit ; $ count = 0 ; for ( $ i = 0 ; $ i < ( $ len - 1 ) ; $ i ++ ) { $ twoDigit = ( $ n [ $ i ] - '0' ) * 10 + ( $ n [ $ i + 1 ] - '0' ) ; if ( $ twoDigit % 4 == 0 ) $ count ++ ; } $ twoDigit = ( $ n [ $ len - 1 ] - '0' ) * 10 + ( $ n [ 0 ] - '0' ) ; if ( $ twoDigit % 4 == 0 ) $ count ++ ; return $ count ; } $ n = "4834" ; echo " Rotations : ▁ " , countRotations ( $ n ) ; ? >