Skip to content

Commit

Permalink
Created Power Functions
Browse files Browse the repository at this point in the history
Created Iterative and Recursive Function to fix issue div-bargali#791
  • Loading branch information
mamadou-diallo committed Oct 28, 2020
1 parent eb83b47 commit 77c4796
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions C++/Algorithms/Mathematical/Power_Func.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
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;


}

0 comments on commit 77c4796

Please sign in to comment.