Find unit digit of x raised to power y | Efficient C ++ program to find unit digit of x ^ y . ; Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int unitDigitXRaisedY ( int x , int y ) { int res = 1 ; for ( int i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } int main ( ) { cout << unitDigitXRaisedY ( 4 , 2 ) ; return 0 ; }