Page 364 - computer science (868) class 11
P. 364

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        void print()

                14        { int r;

                15          double res;
                16          if(n >= 0)   // if power is positive then an
                17          {  r = calpower(a, n);

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

                20        else
                21          { res = 1.0/calpower(a, Math.abs(n));  // if power is negative 1/a             n

                22             System.out.println(a+"^"+n+" = "+res);
                23          }

                24        }
                25      public static void main(int b, int x)  // main method

                26        { Power ob = new Power();
                27          ob.input(b, x);

                28          ob.print();
                29       }

                30      }

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













                362362  Touchpad Computer Science-XI
   359   360   361   362   363   364   365   366   367   368   369