Modular exp code

Why this code gives error?

#include
#include
using namespace std;
int main() {
long int a,b,c;
cin>>a>>b>>c;
cout<<pow(a,b)%c<<endl;
return 0;
}
While the following code executes correctly for some test cases (but not for all) and the difference is only that, in the following code I am taking another variable ‘res’:

#include
#include
using namespace std;
int main() {
long int a,b,c,res;
cin>>a>>b>>c;
res=pow(a,b);
res=res%c;
cout<<res<<endl;
return 0;
}
what would be the possible correction for this code?

The error was arising because of the % operator.

Modulus % operator is applied to integer operands only.

Thus, pow(a,b) was creating an invalid operand type error.

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 am marking this doubt as resolved as you have not replied to this thread for 7 days.
In case, you still have some doubts regarding the same problem, you can reopen this doubt.

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.