1. Sum of Array Elements: Write a Java program to calculate the sum of all elements in an integer array.
    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);
}
}

Explanation:

  1. Input for Number of Elements:

  2. Array Initialization:

  3. Input for Array Elements:

  4. Calculating the Sum:

  5. Output the Sum:

    1. Find the Maximum Element: Write a Java program to find the maximum element in an integer array.
    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);
    
        }
    }
    
    

    Explanation:

    1. Initialization:

      • Initialize max with the first element of the array (arr[0]). This ensures that max is correctly set even if all elements are negative.
    2. Loop:

      • Start the loop from the second element (i = 1) because the first element is already considered for initialization.
      • Compare each element with max, and if the current element is greater than max, update max.
    3. Output:

      • Print the maximum element after the loop completes.
      1. Find the Minimum Element: Write a Java program to find the minimum element in an integer array.
          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);
      }
      }
      

      Explanation Recap:

      1. Initialization:

        • int min = arr[0]; sets the initial minimum value to the first element of the array.
      2. Loop:

        • The loop starts from i = 1 because the first element has already been considered for initialization.
        • For each element in the array, it checks if the current element is smaller than the current min. If so, it updates min.
      3. Output:

        • After the loop completes, the program prints the smallest element found in the array.
        1. Find the Average of Array Elements: Write a Java program to calculate the average of all elements in an integer array.
        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);
            }
        }
        
        

        Explanation:

        1. Initialization:
          • int n = arr.length; correctly sets n to the number of elements in the array.
        2. Sum Calculation:
          • The loop iterates over all elements in the array, adding each element to sum.
        3. Average Calculation:
          • double average = (double) sum / n; ensures that the division is done in floating-point to maintain precision.
        4. Output:
          • The program prints the calculated average of the array elements.

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

      Explanation:

      1. Initialization:

        • int start = 0; and int end = arr.length - 1; set the initial positions for the start and end of the array.
      2. Reversing the Array:

        • The while loop swaps elements from the start and end of the array moving towards the center.
        • The swap is done using a temporary variable temp.
        • After swapping, start is incremented and end is decremented until start is no longer less than end.
      3. Printing the Reversed Array:

        • A for loop iterates through the array and prints each element.
      4. 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");
            }
        }
    }
    
    

    Explanation:

    1. Initialization:
      • boolean isSorted = true; initializes a flag to assume the array is sorted.
      • The for loop iterates through the array from the first element to the second-to-last element.
    2. Check Sorted Condition:
      • Inside the loop, it checks if the current element arr[i] is greater than the next element arr[i + 1].
      • If this condition is true, the array is not sorted, so it sets isSorted to false and breaks out of the loop.
    3. Output:
      • After the loop, it checks the isSorted flag.
      • If isSorted is still true, it prints "The array is sorted." Otherwise, it prints "The array is not sorted."

    Handling Arrays of Different Lengths

    Example Usage

    1. Count Even and Odd Numbers: Write a Java program to count the number of even and odd elements in an integer array.
    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);
        }
    }
    
    

    Explanation:

    1. Counting Even and Odd Elements:

      • Two variables, evenCount and oddCount, are initialized to count the even and odd elements, respectively.
      • The 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.
    2. Printing the Counts:

      • After counting, the program prints the number of even and odd elements with clear labels.
    3. 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);
                }
            }
        }
    }