954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Guys Need Some Help... What is the code for exponent in java?

For:
Sum = Var1 + Var2
Difference = Var1 - Var2
Quotient = Var1 / Var2
Product = Var1 * Var2

What
Exponent = ?

Thanks in advance...

ocreds
Newbie Poster
5 posts since Jul 2008
Reputation Points: 5
Solved Threads: 0
 

I'm assuming you're not allowed to use the Math library or recursion.

If that's the case, use--

static double power(double value, short exponent){
       if(exponent >= 0){ // if exponent is greater than or equal to zero...
       double result = 1; // set result to one

       for(short i = 0; i < exponent; i++){ // doing something exponent times
             result = result * value; // result is assigned the number of itself times value
       }     
       
       return result; // return the result
       }else return 0; // exponent was less than 0 - return 0 for simplicity.
}
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

What if exponent is a double itself :-)

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 
What if exponent is a double itself :-)

Then use the Math Library =P

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You