Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int factorial(int n) {
- if (n < 1)
- return 0;
- if (n == 1)
- return 1;
- int res = 1;
- for (; n > 1; n--)
- res *= n;
- return res;
- }
- int fun1(int n) {
- if (n == 1)
- return 1;
- return n * fun1(n-1);
- }
- int sum(int n) {
- if (n == 1) return 1;
- return n + sum(n - 1);
- }
- int sumArr(int arr[], int sz) {
- if (sz == 1) return arr[0];
- return arr[sz - 1] + sumArr(arr, sz-1);
- }
- void showArr(int arr[], int sz) {
- if (sz == 1)
- cout << arr[0];
- else {
- cout << arr[sz - 1] << " ";
- showArr(arr, sz - 1);
- }
- }
- double power(double x, int n) {
- if (n == 0) return 1;
- if (n == 1) return x;
- return x * power(x, n - 1);
- }
- void main() {
- cout << "Iteration VS Recursion" << endl;
- cout << "5! = " << factorial(5) << endl;
- cout << "5! = " << fun1(5) << endl;
- cout << "3^4 = " << power(3, 4) << endl;
- cout << sum(5) << endl;
- int arr[4]{ 3, 5, 2, 1 };
- cout << sumArr(arr, 4) << endl;
- showArr(arr, 4);
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment