Find Count, Min, Max and Average values from the given array in java Description:…
- User
Find Count, Min, Max and Average values from the given array in java Description: Consider an array with N number of multiple integers. Find below values -
The element with the Minimum value
The element with the Maximum value
Average of all array elements
Total number of array elements (Count).
Sample input : arrNum = {31, 42, 25, 21, 56, 61, 13, 45, 76, 55}
Output : The element with the Minimum value: 13
The element with the Maximum value: 76
Average of all array elements: 42
Total number of array elements: 10
- Assistant
Solution:
int[] arrNum = {31, 42, 25, 21, 56, 61, 13, 45, 76, 55}; int min = arrNum[0]; int max = arrNum[0]; int sum = 0; for(int i=0; i<arrNum.length; i++){ if(arrNum[i]<min){ min = arrNum[i]; } if(arrNum[i]>max){ max = arrNum[i]; } sum += arrNum[i]; } double avg = (double)sum/arrNum.length; int count = arrNum.length; System.out.println("The element with the Minimum value: "+min); System.out.println("The element with the Maximum value: "+max); System.out.println("Average of all array elements: "+avg); System.out.println("Total number of array elements: "+count);
Output:
The element with the Minimum value: 13
The element with the Maximum value: 76
Average of all array elements: 42.0
Total number of array elements: 10
- User
write a program in java to fiter records from array
- Assistant
Here's an example program in Java to filter records from an array based on given condition:
public class FilterArrayRecords { public static void main(String[] args) { String[][] data = { {"John Doe", "24", "Male"}, {"Jane Smith", "32", "Female"}, {"Bob Johnson", "45", "Male"}, {"Mary Williams", "29", "Female"}, {"Tom Brown", "38", "Male"}, {"Lisa Lee", "27", "Female"} }; String gender = "Female"; // set the condition to filter records int count = 0; System.out.println("Filtered records:"); for (int i = 0; i < data.length; i++) { if (data[i][2].equals(gender)) { // check if the record satisfies the condition System.out.println(data[i][0] + " | " + data[i][1] + " | " + data[i][2]); count++; } } System.out.println("Total records found: " + count); } } - User
find even numbers fro array in java program
- Assistant
Here's a Java program to find even numbers from an array:
public class FindEvenNumbers { public static void main(String[] args) { int[] arr = {5, 8, 9, 6, 4, 2, 11}; System.out.print("Even numbers in the array: "); for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { System.out.print(arr[i] + " "); } } } }In this program, we first define an integer array
arrcontaining some elements.We then loop through the array and check if each element is even (i.e. divisible by 2 with no remainder). If it is, we print it.
This program will output:
Even numbers in the array: 8 6 4 2Note that we are only printing the even numbers, not their indices or positions in the array. If you also want to print the positions of the even numbers, you can modify the loop as follows:
for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { System.out.println("Even number " + arr[i] + " found at index " + i); } }This will output: