The problem statement requires me to find out the last digit of a^b. The constraints are that 0 <= a <= 20 and 0 <= b <= 2,147,483,000.
My code works pretty well for numbers like 12^8 or 6^9 or something similar. But when I move to the large number territory, something like 14^1234 or 17^148713, I am always getting an output of -8.
#include <stdio.h>
#include <math.h>
int main() {
int t;
scanf("%d", &t);
while(t--)
{
int a;
long long int b;
double x, y, res;
scanf("%d %lld", &a, &b);
x=(double)a;
y=(double)b;
res = pow(x, y);
int rem;
rem = (int)res%10;
printf("%d\n", rem);
}
return 0; }
What could be the reasons for such a weird output?
Is there no way out apart from storing the big numbers in an array (I guess something like How to calculate 2 to the power 10000000)?