Creating a recursive function can be accomplished in two steps.
The following illustrates a simple function that computes the factorial of N (i.e. N!). The base case is N = 1 or 1! which evaluates to 1. The base case is written as if (N <= 1) { fact = 1; }. The recursive case is used for N > 1, and written as else { fact = N * NFact( N - 1 ); }.
#include<iostream>
using namespace std;
int main()
{
int N;
cout << "Please, a number of elements: ";
cin >> N;
int* arr = new int[N];
cout << "Enter elements: ";
for (int i = 0; i < N; i++)
{
cin>> arr[i];
}
cout << "\nThe even numbers are ";
for (int i = 0; i < N; i++)
{
if (arr[i] % 2 == 0)
cout << arr[i] << " ";
}
}
Comments
Leave a comment