Write a C program to find the array type that needs to be checked to determine whether the elements in a given array are even or odd, or both have.
The user needs to input an integer array and then display the type of the array.
Example 1 − Input: 5 3 1, output: odd array.
Example 2 − Input: 2 4 6 8, output: even array.
Example 3 − Input: 1 2 3 4 5, output: mixed array.
Refer to the algorithm given below to find the array type entered by the user.
Step 1 − Read the size of the array at runtime.
Step 2 − Enter the array elements.
Step 3 − If all elements of the array are odd, print "odd".
Step 4 − If all elements of the array are even numbers, print "even".
Step 5 − Otherwise, print "Mixed".
The following is a C program to find the array type entered by the user−
Demonstration
#include<stdio.h> int main(){ int n; printf("enter no of elements:"); scanf("%d",&n); int arr[n]; int i; int odd = 0, even = 0; printf("enter the elements into an array:</p><p>"); for(i = 0; i < n; i++){ scanf("%d",&arr[i]); } for(i = 0; i < n; i++){ if(arr[i] % 2 == 1) odd++; if(arr[i] % 2 == 0) even++; } if(odd == n) printf("Odd Array"); else if(even == n) printf("Even Array"); else printf("Mixed Array"); return 0; }
When the above When the program is executed, it produces the following output −
Run 1: enter no of elements:5 enter the elements into an array: 2 4 8 10 12 Even Array Run 2: enter no of elements:5 enter the elements into an array: 1 23 45 16 68 Mixed Array
The above is the detailed content of C program to find array type entered by user. For more information, please follow other related articles on the PHP Chinese website!