Write a C++ program to count even number of elements in a given array of integers

  • برمجة سي بلس بلس
  • برمجة

Write a C++ program to count even number of elements in a given array of integers

Sample Output:

2
6

الأجوبة

#include <iostream>
using namespace std;

static int test(int nums[], int arr_length)
          {
            int evens = 0;

            for (int i = 0; i < arr_length; i++)
            {
                if (nums[i] % 2 == 0) evens++;
            }
            return evens;
          }    
               
int main() 
 {  
  int nums1[] = {1, 5, 7, 9, 10, 12};
  int arr_length = sizeof(nums1) / sizeof(nums1[0]);	
  cout << test(nums1, arr_length) << endl; 
  int nums2[] = {0, 2, 4, 6, 8, 10};
  arr_length = sizeof(nums2) / sizeof(nums2[0]);	
  cout << test(nums2, arr_length) << endl;  
  return 0;    
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...