DEV Community

Discussion on: Daily Challenge #250 - Last Digit of a Large Number

Collapse
 
andreasjakof profile image
Andreas Jakof • Edited

Maybe there is some faster mathematical way. But this should do it.

public static int LastDigitOfAPowerB(int a, int b)
{
   if (b == 0) return 1;
   long res = a % 10;
   for(int i =1; i<b;++i)
   { 
         res = (res * a) % 10;
    }
   return (int)res;
}