training

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

mystery.c (509B)


      1 /* Exercise 4.48 */
      2 
      3 #include <stdio.h>
      4 
      5 int mystery(int, int);
      6 
      7 int main()
      8 {
      9    int x, y;
     10 
     11    printf("Enter two integers: ");
     12    scanf("%d%d", &x, &y);
     13 
     14    printf("The result is %d\n", mystery(x, y));
     15 
     16    return 0;
     17 } /* E0F main */
     18 
     19 /* multiplies a for b times recursively */
     20 int mystery(int a, int b)
     21 {
     22    if(b == -1)
     23       return -a;
     24    else if(b == 1)
     25       return a;
     26    else {
     27       if(b > 1)
     28          return a + mystery(a, b - 1);
     29       else
     30 	 return -a + mystery(a, b + 1);
     31    }
     32 } /* eof mystery() */
     33