forked from div-bargali/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created Iterative and Recursive Function to fix issue div-bargali#791
- Loading branch information
1 parent
eb83b47
commit 77c4796
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
} |