Program to find the array type (even, odd or mixed array) | faceprep

Program to find the array type (odd, even or mixed array) is discussed here. Given an array of integers, display the type of the array.

Input format:
  • Input consists of 1 integer and 1 array.
  • Integer corresponds to the size of an array.
Sample Input:

5
2
4
1
3
5

Sample output:

Mixed

Algorithm to find the array type (even, odd or mixed array)

  • Input the number of elements of the array.
  • Input the array elements.
  • If all the elements of the array are odd, display “Odd”.
  • If all the elements of the array are even, display “Even”.
  • Else, display “Mixed”.

Program to find the array type (even, odd or mixed array) is given below.

/* C Program to find the array type (even, odd or mixed array) */
#include<stdio.h>
int main()
{
//fill the code;
int n;
scanf(“%d”,&n);
int arr[n];
int i;
int odd = 0, even = 0;
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”);
else if(even == n)
printf(“Even”);
else
printf(“Mixed”);
return 0;
}

#include<stdio.h>
#include<stdlib.h>
int*createArray (int);
void readArray(int,int *);
int findType(int , int *);
int main()
{
int *a,n,c=0,d=0;
printf(“Enter the size of array\n”);
scanf(“%d”,&n);
printf(“Enter the elements of array\n”);
createArray(n);
readArray(n,a);
findType(n,a);
return 0;
}
int *createArray(int n)
{
int *a;
a=(int*)malloc(n*sizeof(int));
return a;
}
void readArray(int n,int *a)
{
for(int i=0;i<n;i++)
{
scanf(“%d”,a+i);
}}
int findType(int n, int *a)
{
int c=0,d=0;
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
{
c++;
}
else
{
d++;
}}
if(c==n)
{
printf(“The array type is Even\n”);
}
if(d==n)
{
printf(“The array type is Odd\n”);
}
if(c!=n && d!=n)
{
printf(“The array type is Mixed\n”);
}

return 0; }

/* C++ Program to find the array type (even, odd or mixed array) */
#include<iostream>
using namespace std;

int main()
{
//fill your code
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
int i;
int odd = 0, even = 0;
for(i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
odd++;
if(arr[i] % 2 == 0)
even++;
}
if(odd == n)
cout << “Odd”;
else if(even == n)
cout << “Even”;
else
cout << “Mixed”;
return 0;
}

/* Java program to find the array type (even, odd or mixed array) */

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int[]arr = new int[n];
for(int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
int odd =0, even = 0;
for(int i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
odd++;
else
even++;
}
if(odd == n)
System.out.print(“Odd”);
else if(even == n)
System.out.print(“Even”);
else
System.out.print(“Mixed”);

}
}

# Python program find the array type (even, odd or mixed array)
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
odd = 0
even = 0
for i in range(0,n):
if(arr[i] % 2 == 1):
odd = odd + 1
else:
even = even + 1
if(odd == n):
print(“Odd”)
elif(even == n):
print(“Even”)
else:
print(“Mixed”);

Output:

find the array type