training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

power.c (428B)


      1 /* Exercise 5. 37 */
      2 
      3 #include <stdio.h>
      4 
      5 int power(int, int);
      6 
      7 int main()
      8 {
      9    int base, exponent;
     10 
     11    printf("Give me a base and exponent: ");
     12    scanf("%d%d", &base, &exponent);
     13 
     14    printf("Result: %d\n", power(base, exponent));
     15 
     16    return 0;
     17 } /* E0F main */
     18 
     19 /* calculate power of an integer (recursively) */
     20 int power(int b, int e)
     21 {
     22    if(e == 1)
     23       return b * e;
     24 
     25    return b * power(b, e - 1);
     26 
     27 } /* eof power() */
     28