Skip to content

Commit 88726b9

Browse files
realDuYuanChaogithub-actionsPanquesito7
authored
Added math function power (#604)
* added power algorithm * updating DIRECTORY.md * make test function static Co-authored-by: David Leal <[email protected]> * Update misc/power_recursion.c Co-authored-by: David Leal <[email protected]> * Update misc/power_recursion.c Co-authored-by: David Leal <[email protected]> * Update misc/power.c Co-authored-by: David Leal <[email protected]> * Update misc/power.c Co-authored-by: David Leal <[email protected]> * Update misc/power.c Co-authored-by: David Leal <[email protected]> * Update misc/power.c Co-authored-by: David Leal <[email protected]> * Update misc/power_recursion.c Co-authored-by: David Leal <[email protected]> * Update misc/power_recursion.c Co-authored-by: David Leal <[email protected]> * Update misc/power.c Co-authored-by: David Leal <[email protected]> * Update misc/power_recursion.c Co-authored-by: David Leal <[email protected]> * Update misc/power_recursion.c Co-authored-by: David Leal <[email protected]> * Update misc/power.c Co-authored-by: David Leal <[email protected]> * Update misc/power.c Co-authored-by: David Leal <[email protected]> * Update misc/power_recursion.c Co-authored-by: David Leal <[email protected]> * Update misc/power.c Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <[email protected]>
1 parent 49e8f4a commit 88726b9

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@
249249
* [Palindrome](https://github.com/TheAlgorithms/C/blob/master/misc/palindrome.c)
250250
* [Pid](https://github.com/TheAlgorithms/C/blob/master/misc/pid.c)
251251
* [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)
252254
* [Prime](https://github.com/TheAlgorithms/C/blob/master/misc/prime.c)
253255
* [Prime Factoriziation](https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c)
254256
* [Quartile](https://github.com/TheAlgorithms/C/blob/master/misc/quartile.c)

misc/power.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

misc/power_recursion.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)