#include<bits/stdc++.h>
int power(int a ,int n)
{
int val;
//base case
if(n==0)
return 1;
//rec call
else if(n%2==0)
{
val =power(a ,n/2);
val=valval;
}
else{
val=apower(a,n/2);
}
return val;
}
using namespace std;
int main() {
int a,b,c;
cin>>a>>b>>c;
int val=power(a,b);
cout<<val%c;
return 0;
}
Some of the test cases are not passing
hey @siddhant_samal please share your code using ide.codingblocks.com so that i can help you to debug your code.
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.
cant we do it using by recursion??
hello @siddhant_samal
yeah u can.
int exponentMod(int A, int B, int C)
{
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (int)((y + C) % C);
}