Canna

Tuesday, 25 September 2018

Evenodd

  • A homework was given to UKG student Latha to count total number of even and odd elements from a set of input. Can you help Latha to complete this assignment? Hint - C program using array concept
  • Test Case 1

    Input (stdin)
    9
    
    1 2 3 4 5 6 7 8 9
    
    
    Expected Output
    4
    
    5
  • Test Case 2

    Input (stdin)
    3
    
    10 20 30
    
    
    Expected Output
    3
    
    0
  • Program
  • #include <stdio.h>
    
    #define MAX_SIZE 100 //Maximum size of the array
    
    int main()
    {
        int arr[MAX_SIZE];
        int i, size, even, odd;
    
        /* Input size of the array */
        scanf("%d", &size);
    
        /* Input array elements */
      
        for(i=0; i<size; i++)
        {
            scanf("%d", &arr[i]);
        }
    
        /* Assuming that there are 0 even and odd elements */
        even = 0;
        odd  = 0;
    
        for(i=0; i<size; i++)
        {
            /* If the current element of array is even then increment even count */
            if(arr[i]%2 == 0)
            {
                even++;
            }
            else
            {
                odd++;
            }
        }
    
        printf("%d\n", even);
        printf("%d", odd);
    
        return 0;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.