fork download
  1. #include <iostream>
  2.  
  3. // Recursive function to count even numbers in the array
  4. int countEven(int arr[], int size) {
  5. // Base case: if the size is 0, return 0
  6. if (size == 0) {
  7. return 0;
  8. }
  9.  
  10. // Check if the last element is even
  11. int isEven = (arr[size - 1] % 2 == 0) ? 1 : 0;
  12.  
  13. // Recursive call for the rest of the array
  14. return isEven + countEven(arr, size - 1);
  15. }
  16.  
  17. int main() {
  18. int a[] = {1, 4, 6, 3, 7, 11, 8, 9, 10};
  19. int size = sizeof(a) / sizeof(a[0]);
  20.  
  21. int evenCount = countEven(a, size);
  22.  
  23. std::cout << "The count of even numbers in the array is: " << evenCount << std::endl;
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
The count of even numbers in the array is: 4