File tree Expand file tree Collapse file tree 3 files changed +89
-0
lines changed Expand file tree Collapse file tree 3 files changed +89
-0
lines changed Original file line number Diff line number Diff line change 249
249
* [ Palindrome] ( https://github.com/TheAlgorithms/C/blob/master/misc/palindrome.c )
250
250
* [ Pid] ( https://github.com/TheAlgorithms/C/blob/master/misc/pid.c )
251
251
* [ Poly Add] ( https://github.com/TheAlgorithms/C/blob/master/misc/poly_add.c )
252
+ * [ Power] ( https://github.com/TheAlgorithms/C/blob/master/misc/power.c )
253
+ * [ Power Recursion] ( https://github.com/TheAlgorithms/C/blob/master/misc/power_recursion.c )
252
254
* [ Prime] ( https://github.com/TheAlgorithms/C/blob/master/misc/prime.c )
253
255
* [ Prime Factoriziation] ( https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c )
254
256
* [ Quartile] ( https://github.com/TheAlgorithms/C/blob/master/misc/quartile.c )
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @file
3
+ * @brief Program to calculate
4
+ * [exponentiation](https://en.wikipedia.org/wiki/Exponentiation)
5
+ *
6
+ * @author [Du Yuanchao](https://github.com/shellhub)
7
+ */
8
+ #include <assert.h>
9
+
10
+ /**
11
+ * @brief Returns the value of the first argument raised to the power of the
12
+ * second argument.
13
+ * @param a the base.
14
+ * @param b the exponent.
15
+ * @returns the value {@code a}<sup>{@code b}</sup>.
16
+ */
17
+ long power (int a , int b )
18
+ {
19
+ long result = 1 ;
20
+ for (int i = 1 ; i <= b ; ++ i )
21
+ {
22
+ result *= a ;
23
+ }
24
+ return result ;
25
+ }
26
+
27
+ /**
28
+ * @brief Test function
29
+ * @return void
30
+ */
31
+ static void test ()
32
+ {
33
+ assert (power (0 , 2 ) == 0 );
34
+ assert (power (2 , 3 ) == 8 );
35
+ assert (power (2 , 10 ) == 1024 );
36
+ assert (power (3 , 3 ) == 27 );
37
+ }
38
+
39
+ /**
40
+ * @brief Driver Code
41
+ * @returns 0 on exit
42
+ */
43
+ int main ()
44
+ {
45
+ test ();
46
+ return 0 ;
47
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @file
3
+ * @brief Program to calculate
4
+ * [exponentiation](https://en.wikipedia.org/wiki/Exponentiation) using
5
+ * recursion algorithm.
6
+ *
7
+ * @author [Du Yuanchao](https://github.com/shellhub)
8
+ */
9
+ #include <assert.h>
10
+
11
+ /**
12
+ * @brief Returns the value of the first argument raised to the power of the
13
+ * second argument using recursion.
14
+ * @param a the base.
15
+ * @param b the exponent.
16
+ * @returns the value {@code a}<sup>{@code b}</sup>.
17
+ */
18
+ long power (int a , int b ) { return b == 0 ? 1 : a * power (a , b - 1 ); }
19
+
20
+ /**
21
+ * @brief Test function
22
+ * @return void
23
+ */
24
+ static void test ()
25
+ {
26
+ assert (power (0 , 2 ) == 0 );
27
+ assert (power (2 , 3 ) == 8 );
28
+ assert (power (2 , 10 ) == 1024 );
29
+ assert (power (3 , 3 ) == 27 );
30
+ }
31
+
32
+ /**
33
+ * @brief Driver Code
34
+ * @returns 0 on exit
35
+ */
36
+ int main ()
37
+ {
38
+ test ();
39
+ return 0 ;
40
+ }
You can’t perform that action at this time.
0 commit comments