Print last k digits of a ^ b ( a raised to power b ) | Java code to find last k digits of a ^ b ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Method to print last k digits of a ^ b ; Generating 10 ^ k ; Calling modular exponentiation ; Printing leftmost zeros . Since ( a ^ b ) % k can have digits less then k . In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver Method
public class GFG { static int power ( long x , long y , long p ) { long res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return ( int ) res ; } static void printLastKDigits ( int a , int b , int k ) { System . out . print ( " Last ▁ " + k + " ▁ digits ▁ of ▁ " + a + " ^ " + b + " ▁ = ▁ " ) ; int temp = 1 ; for ( int i = 1 ; i <= k ; i ++ ) temp *= 10 ; temp = power ( a , b , temp ) ; for ( int i = 0 ; i < k - Integer . toString ( temp ) . length ( ) ; i ++ ) System . out . print ( 0 ) ; if ( temp != 0 ) System . out . print ( temp ) ; } public static void main ( String [ ] args ) { int a = 11 ; int b = 3 ; int k = 2 ; printLastKDigits ( a , b , k ) ; } }