fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Test t = new Test(2);
  14. System.out.println(t.power(5));
  15. }
  16. }
  17.  
  18. class Test
  19. {
  20. int n;
  21. Test(int n)
  22. {
  23. this.n = n;
  24. }
  25. public int power(int p) {
  26. int x = 0;
  27.  
  28. if (p == 0) {
  29. return 1;
  30. }
  31. if (p == 1) {
  32. return this.n;
  33. }
  34. if (p % 2 == 0) {
  35. x = this.power(p / 2);
  36. return x * x;
  37. }
  38. else {
  39. return this.n * this.power(p - 1);
  40. }
  41.  
  42.  
  43. }}
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
32