Page 403 - Computer science 868 Class 12
P. 403

5          n = n1;
                   6        }

                   7        int calpower(int aa, int nn)
                   8        { if(nn == 0)  // base case
                   9            return 1;

                  10          else

                  11            return aa*calpower(aa, nn-1); // recursive case
                  12        }
                  13

                  14        void print()
                  15        { int r;

                  16          double res;
                  17          if(n >= 0)   // if power is positive then a         n

                  18          {  r = calpower(a, n);
                  19             System.out.println (a+"^"+n+" = "+r);

                  20          }
                  21        else

                  22          { res = 1.0/calpower(a, Math.abs(n));  // if power is negative 1/a             n
                  23             System.out.println(a+"^"+n+" = "+res);

                  24          }
                  25        }

                  26
                  27      public static void main(int b, int x)  // main method

                  28        { Power ob = new Power();
                  29          ob.input(b, x);

                  30          ob.print();
                  31       }

                  32      }

                 The output of the preceding program is as follows:
                 Output 1
                 2^5 = 32
                 Output 2
                 2^-3 = 0.125








                                                                                                                       401
                                                                                                           Recursion   401
   398   399   400   401   402   403   404   405   406   407   408