Finding the array type

Write a program to find the array type.
Type of Array: If all numbers in the array are odd then Odd, if all numbers in the array are even then Even array otherwise the array contains both even and odd.
Example:
Input:  arr[]={1,2,3,4,5}
Output: Both

C Program

#include <stdio.h>

int main()
{
    int arr[] = {12345};

    int n = sizeof(arr) / sizeof(arr[0]);

    int oddCount = 0;
    int evenCount = 0;
    
    for (int i = 0i < ni++)
    {
        if (arr[i] % 2 == 0)
        {
            evenCount++;
        }
        else
        {
            oddCount++;
        }
    }
    if (evenCount > 0 && oddCount > 0)
    {
        printf("Both\n");
    }
    else if (evenCount > 0)
    {
        printf("Even\n");
    }
    else
    {
        printf("Odd\n");
    }
    return 0;
}


No comments:

Post a Comment