package Arrays;
import java.util.Scanner;
public class SumArrayElements {
public static void main(String[] args) {
Scanner sc = new Scanner([System.in](<http://system.in/>));
System.out.println("Enter the number of elements in the array:");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("The sum of all elements in the array is: " + sum);
}
}
Input for Number of Elements:
sc.nextInt() to read this number.Array Initialization:
Input for Array Elements:
Calculating the Sum:
Output the Sum:
package Arrays;
public class maxElement {
public static void main(String[] args) {
int [] arr = {1,2,3,4,5};
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i]>max){
max=arr[i];
}
}
System.out.println("Max element is :"+max);
}
}
Initialization:
max with the first element of the array (arr[0]). This ensures that max is correctly set even if all elements are negative.Loop:
i = 1) because the first element is already considered for initialization.max, and if the current element is greater than max, update max.Output:
package Arrays;
public class minElement {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 52, 0};
// Initialize min with the first element of the array
int min = arr[0];
// Start loop from the second element
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println("Min element is: " + min);
}
}
Initialization:
int min = arr[0]; sets the initial minimum value to the first element of the array.Loop:
i = 1 because the first element has already been considered for initialization.min. If so, it updates min.Output:
package Arrays;
public class avg {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
int n = arr.length;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
// Calculate the average using floating-point division
double average = (double) sum / n;
System.out.println("The average of the array elements is: " + average);
}
}
int n = arr.length; correctly sets n to the number of elements in the array.sum.double average = (double) sum / n; ensures that the division is done in floating-point to maintain precision.5. Reverse an Array: Write a Java program to reverse the elements of an integer array in-place.
package Arrays;
public class rev {
public static void main(String[] args) {
int[] arr = {3, 4, 5, 6, 7};
int start = 0;
int end = arr.length - 1;
// Reverse the array in place
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
// Print the reversed array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Initialization:
int start = 0; and int end = arr.length - 1; set the initial positions for the start and end of the array.Reversing the Array:
while loop swaps elements from the start and end of the array moving towards the center.temp.start is incremented and end is decremented until start is no longer less than end.Printing the Reversed Array:
for loop iterates through the array and prints each element.Check if Array is Sorted: Write a Java program to check if an integer array is sorted in non-decreasing order.
package Arrays;
public class sort {
public static void main(String[] args) {
int [] arr = {1,2,3,4,5};
boolean isval = true;
for (int i = 0; i < arr.length-1; i++) {
if(arr[i]>arr[i+1]){
isval = false;
break;
}
}
if(isval){
System.out.println("Array is sorted");
}else {
System.out.println("Array is not sorted");
}
}
}
boolean isSorted = true; initializes a flag to assume the array is sorted.for loop iterates through the array from the first element to the second-to-last element.arr[i] is greater than the next element arr[i + 1].isSorted to false and breaks out of the loop.isSorted flag.isSorted is still true, it prints "The array is sorted." Otherwise, it prints "The array is not sorted."arr array to test different scenarios, such as arrays that are already sorted, arrays that are not sorted, or arrays with negative numbers and duplicates.package Arrays;
public class count {
public static void main(String[] args) {
int[] arr = {2, 4, 6, 3, 5, 7};
int evenCount = 0;
int oddCount = 0;
// Count even and odd elements
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
// Print the counts
System.out.println("Number of even elements: " + evenCount);
System.out.println("Number of odd elements: " + oddCount);
}
}
Counting Even and Odd Elements:
evenCount and oddCount, are initialized to count the even and odd elements, respectively.for loop iterates through the array, checking each element's remainder when divided by 2. If the remainder is 0, the element is even; otherwise, it's odd. The counts are incremented accordingly.Printing the Counts:
Find Duplicate Elements: Write a Java program to find duplicate elements in an integer array.
package Arrays;
public class duplicate {
public static void main(String[] args) {
int[] arr = {2, 2, 5, 5, 7, 7, 78, 90, 90};
int val = 0;
// Loop through the array to find duplicate values
for (int i = 0; i < arr.length - 1; i++) {
// Check if the current element is equal to the next element
if (arr[i] == arr[i + 1]) {
// If a duplicate is found, assign the duplicate value to 'val'
val = arr[i];
// Print the duplicate value
System.out.println("Duplicate value: " + val);
}
}
}
}