I am given with three integers A, B and C. I need to implement a program to perform the operation A^B modulus C. I without thinking much, wrote the following code:-
public int Mod(int A, int B, int C)
{
return (int)Math.pow(A,B) % C;
}
testcase : A = -1, B = 1, C = 20, the expected o/p was 19 whereas my code gave -1 as output.
I found out that this approach is incorrect when there is a negative number, in this example, when A < 0. I referred to explanations in few websites, but was unable to understand this behavior. Please help me on this one. Thank you.