Page 390 - Cs_withBlue_J_C11_Flipbook
P. 390
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
388388 Touchpad Computer Science-XI

