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.  
  11. public static double pow(double x, int n) {
  12. if(n == 0)
  13. return 1;
  14. double y = 1;
  15. while (n > 1) {
  16. if (n%2 == 1) {
  17. y *= x;
  18. }
  19. x *= x;
  20. n /= 2;
  21. }
  22. return x*y;
  23. }
  24.  
  25.  
  26. public static void main (String[] args) throws java.lang.Exception
  27. {
  28. System.out.println(pow(2.0, 0));
  29. System.out.println(pow(2.0, 1));
  30. System.out.println(pow(2.0, 2));
  31. System.out.println(pow(2.0, 3));
  32. System.out.println(pow(2.0, 4));
  33. System.out.println(pow(2.0, 5));
  34. }
  35. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
1.0
2.0
4.0
8.0
16.0
32.0