Modular exponentiation issue

In the above problem, we have 3 numbers, a,b and c, and we’ve to find a^b%c. Here’s the code I’ve written:
#include
using namespace std;
int main() {
int a,b,c;
cin>>a>>b>>c;
int power=1,ans;
for(int i=1;i<=b;i++)
{
power*=a;
}
ans=power%c;
cout<<ans;
return 0;
}
Why are 6 of the 10 test cases failing?

Hello @Rohanaggarwal8590,

I apologize for replying late.
I somehow forgot to answer your doubt. I hope, you’ll understand.

The failure of code is due to the following reason:

The maximum value a and b can have is 100000.
What you are are doing is a^b i.e. 100000^100000.

Is there any datatype available to store this big(infinitely big) value?
No, is the answer.

This, is the reason you are getting wrong output. If you want to verify this, then I would suggest you to print ans and see what it print.

Think of a different way to calculate modular exponential.
Hint: study about exp. by squaring method algorithm.

Refer the below mentioned link for details:
https://www.google.com/url?sa=i&source=web&cd=&ved=2ahUKEwiW37bntJTjAhXXWisKHU6SAswQzPwBegQIARAC&url=https%3A%2F%2Fwww.geeksforgeeks.org%2Fexponential-squaring-fast-modulo-multiplication%2F&psig=AOvVaw1vw3oDqn1bMtovjRL8iMdC&ust=1562094281766744

If you still have doubts, feel free to ask.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.