Non-recursive method for finding \$a^n\$
There is a more efficient method using squaring:

int result = 1;
while(n&gt;0){
    if(n%2 == 1)result*=a;
    a *= a;
    n /= 2;
}


Or in recursive notation:

int pow(int base, int exponent) {
     if(exponent == 0) {
          return 1;
     } else if(exponent%2 == 1){
          return base * pow(base*base, exponent / 2 );
     } else {
          return pow(base*base, exponent / 2 );
     }
}


This works because

\$\$ a^n = \begin{cases}
(a^2)^{\frac{n}{2}} &amp; \text{if $n$ is even} \\
a \cdot (a^2)^{\frac{n-1}{2}} &amp; \text{if $n$ is odd}
\end{cases}\$\$