File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ package maths ;
2+
3+ public class Pow {
4+ public static void main (String [] args ) {
5+ assert pow (2 , 0 ) == Math .pow (2 , 0 );
6+ assert pow (0 , 2 ) == Math .pow (0 , 2 );
7+ assert pow (2 , 10 ) == Math .pow (2 , 10 );
8+ assert pow (10 , 2 ) == Math .pow (10 , 2 );
9+ }
10+
11+ /**
12+ * Returns the value of the first argument raised to the power of the
13+ * second argument
14+ *
15+ * @param a the base.
16+ * @param b the exponent.
17+ * @return the value {@code a}<sup>{@code b}</sup>.
18+ */
19+ public static long pow (int a , int b ) {
20+ long result = 1 ;
21+ for (int i = 1 ; i <= b ; i ++) {
22+ result *= a ;
23+ }
24+ return result ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package Maths ;
2+
3+ public class PowRecursion {
4+ public static void main (String [] args ) {
5+ assert pow (2 , 0 ) == Math .pow (2 , 0 );
6+ assert pow (0 , 2 ) == Math .pow (0 , 2 );
7+ assert pow (2 , 10 ) == Math .pow (2 , 10 );
8+ assert pow (10 , 2 ) == Math .pow (10 , 2 );
9+ }
10+
11+ /**
12+ * Returns the value of the first argument raised to the power of the
13+ * second argument
14+ *
15+ * @param a the base.
16+ * @param b the exponent.
17+ * @return the value {@code a}<sup>{@code b}</sup>.
18+ */
19+ public static long pow (int a , int b ) {
20+ int result = 1 ;
21+ for (int i = 1 ; i <= b ; i ++) {
22+ result *= a ;
23+ }
24+ return result ;
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments