Rotate digits of a given number by K | Function to find the count of digits in N ; Stores count of digits in N ; Calculate the count of digits in N ; Update digit ; Update N ; Function to rotate the digits of N by K ; Stores count of digits in N ; Update K so that only need to handle left rotation ; Stores first K digits of N ; Remove first K digits of N ; Stores count of digits in left_no ; Append left_no to the right of digits of N ; Driver Code ; Function Call
function numberOfDigit ( N ) { let digit = 0 ; while ( N > 0 ) { digit ++ ; N = Math . floor ( N / 10 ) ; } return digit ; } function rotateNumberByK ( N , K ) { let X = numberOfDigit ( N ) ; K = ( ( K % X ) + X ) % X ; let left_no = Math . floor ( N / Math . floor ( Math . pow ( 10 , X - K ) ) ) ; N = N % Math . floor ( Math . pow ( 10 , X - K ) ) ; let left_digit = numberOfDigit ( left_no ) ; N = ( N * Math . floor ( Math . pow ( 10 , left_digit ) ) ) + left_no ; document . write ( N ) ; } let N = 12345 , K = 7 ; rotateNumberByK ( N , K ) ;