Program to find last two digits of 2 ^ n | C ++ code to find last 2 digits of 2 ^ n ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; 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 ; C ++ function to calculate number of digits in x ; C ++ function to print last 2 digits of 2 ^ n ; Generating 10 ^ 2 ; Calling modular exponentiation ; Printing leftmost zeros . Since ( 2 ^ n ) % 2 can have digits less then 2. In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; int power ( long long int x , long long int y , long long int p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int numberOfDigits ( int x ) { int i = 0 ; while ( x ) { x /= 10 ; i ++ ; } return i ; } void LastTwoDigit ( int n ) { cout << " Last ▁ " << 2 ; cout << " ▁ digits ▁ of ▁ " << 2 ; cout << " ^ " << n << " ▁ = ▁ " ; int temp = 1 ; for ( int i = 1 ; i <= 2 ; i ++ ) temp *= 10 ; temp = power ( 2 , n , temp ) ; for ( int i = 0 ; i < 2 - numberOfDigits ( temp ) ; i ++ ) cout << 0 ; if ( temp ) cout << temp ; } int main ( ) { int n = 72 ; LastTwoDigit ( n ) ; return 0 ; }