Last digit in a power of 2 | C ++ program to find last digit in a power of 2. ; Corner case ; Find the shift in current cycle and return value accordingly ; return 6 ; When n % 4 == 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastDigit2PowerN ( int n ) { if ( n == 0 ) return 1 ; else if ( n % 4 == 1 ) return 2 ; else if ( n % 4 == 2 ) return 4 ; else if ( n % 4 == 3 ) return 8 ; else } int main ( ) { for ( int n = 0 ; n < 20 ; n ++ ) cout << lastDigit2PowerN ( n ) << " ▁ " ; return 0 ; }