: Use of arrays to find the lowest and highest array value, differentiate the od
ID: 3713480 • Letter: #
Question
: Use of arrays to find the lowest and highest array value, differentiate the odd and even numbers from a given set of values. Problem Description: Write a program to prompt the user to input at least 5 integers. The integers are stored in an array of ints. The program should output the following: a) The highest value in the array. b) The lowest value in the array. c) Array element(s) that are odd numbers d) Array elements(s) that are even values. Note: The array must consist of a minimum of 5 elements or an error will occur. Sample Output: Enter the number of elements in the array. Please enter at least 5: 52 10 12 1 5 My array of elements: 2 10 12 1 5 The highest value is: 12 The lowest value is: 1 Odd numbers are: 1 5 Even numbers are: 2 10 12 Enter the number of elements in the array. Please enter at least 5: 3 Please run the program again and enter at least 5 elements. • Use the name ArrayAnnalyze for the class name. • Use meaningful labels for all outputs. • Follow the programming standards and conventions
Can you please solve and make sure that it works just like the output and no errors in it thank you.
Explanation / Answer
Answer:
import java.util.Scanner;
ArrayAnnalyze.java:
public class ArrayAnnalyze {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int array[]; // Array to store values
int size; // to declare the size of array entered by user
int highest; // to hold the highest value of array
int lowest; // to hold the lowest value of array
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the array. Please enter at least 5: ");
size = sc.nextInt(); // to read input for size from user
while(size<5){ // loop to check entered value is less than 5 then repeat until value is greater or equal to 5
System.out.println("**** You have entered less than 5 please try again ****");
System.out.println("Enter the number of elements in the array. Please enter at least 5: ");
size = sc.nextInt(); // again read input for size from user
}
array = new int[size]; // declaring the size of array
for(int i =0; i<size; i++){ // loop to read input array elements
array[i] = sc.nextInt();
}
System.out.print("My Array of elements :");
for(int i =0; i<size; i++){ // loop to display the array elements
System.out.print(" "+array[i]);
}
highest = array[0]; // Assigning the value of index 0 to highest
for(int i =1; i<size; i++){ // loop to check the highest value and store in variable highest
if(highest < array[i]){
highest = array[i];
}
}
System.out.println(" The highest value is: "+highest);
lowest = array[0]; // Assigning the value of index 0 to lowest
for(int i =1; i<size; i++){ // loop to check the lowest value and store in variable lowest
if(lowest > array[i]){
lowest = array[i];
}
}
System.out.println("The lowest value is: "+lowest);
System.out.print("Odd numbers are: ");
for(int i =0; i<size; i++){ // loop to display odd numbers of array
if(array[i]%2 != 0){
System.out.print(" "+array[i]);
}
}
System.out.print(" Even numbers are: ");
for(int i =0; i<size; i++){ // loop to display even numbers of array
if(array[i]%2 == 0){
System.out.print(" "+array[i]);
}
}
}
}
Output:
Enter the number of elements in the array. Please enter at least 5:
5
2 10 12 5 1
My Array of elements : 2 10 12 5 1
The highest value is: 12
The lowest value is: 1
Odd numbers are: 5 1
Even numbers are: 2 10 12
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.