Advertisement
JoshJurban

Exercise 8.13

Nov 25th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. package chapter8;
  2.  
  3. public class Numeric
  4. {
  5.         public static double intPower(double x, int n)
  6.     {
  7.                 double answer;
  8.                
  9.                 if(n > 0 && n % 2 != 0)
  10.         {
  11.                         double intermediate;
  12.                         intermediate = Math.pow(x, n-1);
  13.                         answer = intermediate * x;
  14.                 }
  15.                
  16.                 if(n < 0){
  17.                         answer = 1 / (Math.pow(x, n));
  18.                 }
  19.  
  20.                 if(n > 0 && n % 2 == 0)
  21.         {
  22.                         double mediator;
  23.                         mediator = Math.pow(x, n / 2);
  24.                         answer = Math.pow(mediator, 2);
  25.                 }
  26.                 return answer;
  27.         }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement