Image

Listens: radiohead - i might be wrong

powers and factorials...

ok! thankyou so so much everyone who offered help. i think i've got them working now.

public static double power (double num1In, int num2In)<br /> {<br /> if(num2In &gt; 1)<br /><br /> return num1In*power(num1In, num2In-1);<br /><br /> else if(num2In &lt; 0)<br /><br /> return 1.0/power(num1In, -num2In);<br /><br /> else<br /><br /> if (num2In == 0)<br /><br /> return (1);<br /><br /> else<br /><br /> return (num1In);<br /> }<br /><br /> public static int factorial(int n)<br /> {<br /> return _factorial(n, 1);<br /> }<br /><br /> private static int _factorial(int n, int result)<br /><br /> {<br /> if (n &lt;= 0)<br /><br /> {<br /> return result;<br /> }<br /> else<br /><br /> {<br /> return _factorial(n - 1, n * result);<br /> }<br /> }

i'll probably be back here next semester with possibly something slightly more taxing for you. ;) thanks much again.

---

this is most definitley painfully simple for the most of you i expect but i just can't seem to get my head around it...

i need to write 2 methods, one to calculate powers (2 numbers given) and the other to calculate factorials (1 number given)

honestly,

public static double power (double num1In, double num2Dbl)<br /> {<br /> return (num1In);<br /> }<br /><br /> public static double factorial (double num1In)<br /> {<br /> return (num1In);<br /><br /> //The factorial of 0, written 0!, is 1. The factorial of a positive whole number N, written N!, is N multiplied by the factorial of N - 1.<br /><br /> } is as far as i can get. ;)

i'm really not expecting anyone to code this for me or anything. just a little help with the design, i think the factorial one needs to use recursion? and the powers a loop in a loop?

sometimes i feel i'm really on the wrong degree. :S