From 77c4796ed4bcd631bb8ffb4163b2ce306af956af Mon Sep 17 00:00:00 2001 From: Mamadou Diallo Date: Wed, 28 Oct 2020 12:32:04 -0400 Subject: [PATCH] Created Power Functions Created Iterative and Recursive Function to fix issue #791 --- C++/Algorithms/Mathematical/Power_Func.cpp | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 C++/Algorithms/Mathematical/Power_Func.cpp diff --git a/C++/Algorithms/Mathematical/Power_Func.cpp b/C++/Algorithms/Mathematical/Power_Func.cpp new file mode 100644 index 00000000..be598555 --- /dev/null +++ b/C++/Algorithms/Mathematical/Power_Func.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; + + + + + +//Iterative Function +double powerIterative(double base, unsigned int n) { + double power = 1.0; + + for (int i = 1; i <= n; i++) //For loop + { + power = power * base; + } + + return (power); +} + + +//Recursive function +double powerRecursive(double base, unsigned int n) { + + + if (n != 0) + return (base * powerRecursive(base, n - 1)); //Function calling itself + else + return 1; //base function + +} + +int main(void){ + + + + //Iterative Function + + cout << "Iterative Function" << endl; + cout << "Please enter a number" << endl; + double iF; + cin >> iF; + double answerIterative = powerIterative(2, iF); + cout << answerIterative << endl; + + + //Recursive Function + cout << "Recursive Function" << endl; + cout << "Please enter a number" << endl; + double rF; + cin >> rF; + double answerRecursion = powerRecursive(2, rF); + cout << answerRecursion << endl; + + +}