Answer:
In Java:
import java.util.*;
public class Main{
public static boolean isArrayEven(int[] arrayValues, int arraySize){
boolean val = true;
int odd = 0;
for(int i =0;i<arraySize;i++){
if(arrayValues[i]%2!=0){
odd++; }}
if(odd>0){
val = false;}
return val;
}
public static boolean isArrayOdd(int[] arrayValues, int arraySize){
boolean val = true;
int even = 0;
for(int i =0;i<arraySize;i++){
if(arrayValues[i]%2==0){
even++;}}
if(even>0){
val = false;}
return val;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Array Length: ");
int n = input.nextInt();
int[] array = new int[n];
System.out.print("Array Elements: ");
for(int i = 0;i<n;i++){
array[i] = input.nextInt(); }
boolean even = isArrayEven(array,n);
boolean odd = isArrayOdd(array,n);
if(even == true && odd == false){
System.out.print("all even"); }
else if(even == false && odd == true){
System.out.print("all odd"); }
else{
System.out.print("not even or odd"); }
}
}
Step-by-step explanation:
This declares the isArrayEven method
public static boolean isArrayEven(int[] arrayValues, int arraySize){
This initializes the return value to true
boolean val = true;
This initializes the frequency of odd numbers to 0
int odd = 0;
This iterates through the array
for(int i =0;i<arraySize;i++){
This checks if the current array element is odd
if(arrayValues[i]%2!=0){
If yes, the frequency of odd numbers is incremented by 1
odd++; }}
If the frequency of odd is greater than 1, then the array is not an even array
if(odd>0){
val = false;}
This returns true or false
return val;
}
This declares the isArrayOdd method
public static boolean isArrayOdd(int[] arrayValues, int arraySize){
This initializes the return value to true
boolean val = true;
This initializes the frequency of even numbers to 0
int even = 0;
This iterates through the array
for(int i =0;i<arraySize;i++){
This checks if the current array element is even
if(arrayValues[i]%2==0){
If yes, the frequency of even numbers is incremented by 1
even++;}}
If the frequency of even is greater than 1, then the array is not an odd array
if(even>0){
val = false;}
This returns true or false
return val;
}
The main method begins here
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
This prompts for length of array: System.out.print("Array Length: ");
This gets input for length of array int n = input.nextInt();
int[] array = new int[n];
This prompts for array elements: System.out.print("Array Elements: ");
This gets input for the array
for(int i = 0;i<n;i++){
array[i] = input.nextInt();
}
This calls the isArrayEven method and the result is stored in even
boolean even = isArrayEven(array,n);
This calls the isArrayOdd method and the result is stored in odd
boolean odd = isArrayOdd(array,n);
The following prints the result of the methods
if(even == true && odd == false){
System.out.print("all even"); }
else if(even == false && odd == true){
System.out.print("all odd"); }
else{
System.out.print("not even or odd"); }