197k views
7 votes
LAB: Even/odd values in an array

Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers in the list. The first integer is not in the list. Assume that the list will always contain less than 20 integers.
Ex: If the input is:
5 2 4 6 8 10
the output is:
all even Ex:
If the input is:
5 1 -3 5 -7 9
the output is:
all odd
Ex: If the input is:
5 1 2 3 4 5
the output is:
not even or odd
Your program must define and call the following two methods. isArrayEven() returns true if all integers in the array are even and false otherwise. isArrayOdd() returns true if all integers in the array are odd and false otherwise.
public static boolean isArrayEven(int[] arrayValues, int arraySize)
public static boolean isArrayOdd(int[] arrayValues, int arraySize)
LabProgram.java
Load default template
1 import java until Scanner
2
3 public class ropa
4
5 /* Define your method here */
6
7 public static void main(Strist args) {
8 /* Type your code here */
9 }
10
11
11

1 Answer

8 votes

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"); }

User Ianolito
by
7.3k points