Find Last Digit of a ^ b for Large Numbers | Function to find b % a ; Initialize result ; calculating mod of b with a to make b like 0 <= b < a ; return mod ; return modulo ; function to find last digit of a ^ b ; if a and b both are 0 ; if exponent is 0 ; if base is 0 ; if exponent is divisible by 4 that means last digit will be pow ( a , 4 ) % 10. if exponent is not divisible by 4 that means last digit will be pow ( a , b % 4 ) % 10 ; Find last digit in ' a ' and compute its exponent ; Return last digit of result ; Driver program to run test case
function Modulo ( a , b ) { let mod = 0 ; for ( let i = 0 ; i < b . length ; i ++ ) mod = ( mod * 10 + b [ i ] - ' ' ) % a ; } function LastDigit ( a , b ) { let len_a = a . length ; let len_b = b . length ; if ( len_a == 1 && len_b == 1 && b [ 0 ] == ' ' && a [ 0 ] == ' ' ) return 1 ; if ( len_b == 1 && b [ 0 ] == ' ' ) return 1 ; if ( len_a == 1 && a [ 0 ] == ' ' ) return 0 ; exp = ( Modulo ( 4 , b ) == 0 ) ? 4 : Modulo ( 4 , b ) ; res = Math . pow ( a [ len_a - 1 ] - ' ' , exp ) ; return res % 10 ; } let a = " " ; let b = " " ; document . write ( LastDigit ( a , b ) ) ;