Last digit in a power of 2 | Java 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
import java . io . * ; import java . util . * ; class GFG { static 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 } public static void main ( String [ ] args ) { for ( int n = 0 ; n < 20 ; n ++ ) System . out . print ( lastDigit2PowerN ( n ) + " ▁ " ) ; } }