C program to find type of array entered by the user.

CServer Side ProgrammingProgramming

Problem

Write a C Program to find the array type which we need to check, whether the given elements in an array are even numbers or odd numbers or combination of both.

Solution

So, user has to enter an array of integers, 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.

Algorithm

Refer an algorithm given below to find the array type entered by the user.

Step 1 − Read the size of array at runtime.

Step 2 − Input the array elements.

Step 3 − If all the elements of the array are odd, Then, print "Odd".

Step 4 − If all the elements of the array are even, Then, print "Even".

Step 5 − Else, print "Mixed".

Example

Following is the C program to find the array type entered by the user −

 Live Demo

#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:\n");
   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;
}

Output

When the above 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
raja
Published on 26-Mar-2021 07:33:40
Advertisements