Find unit digit of x raised to power y | Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver Code
function unitDigitXRaisedY ( x , y ) { let res = 1 ; for ( let i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } document . write ( unitDigitXRaisedY ( 4 , 2 ) ) ;