JAVA SP T


Lets dive into snapshot of my programs!💻

9.Program to print the largest element in an array.

💡Code:

  public class LargestElementInArrayp4_7978 {
              public static void main(String[] args) {
                  // Example array
                  int[] numbers = {15, 7, 23, 45, 9, 56, 82, 31};
          
                  // Find and print the largest element in the array
                  int largestElement = findLargestElement(numbers);
                  System.out.println("The largest element in the array is: " + largestElement);
              }
          
              // Function to find the largest element in an array
              private static int findLargestElement(int[] array) {
                  if (array.length == 0) {
                      throw new IllegalArgumentException("Array is empty");
                  }
          
                  int largest = array[0];
                  for (int i = 1; i < array.length; i++) {
                      if (array[i] > largest) {
                          largest = array[i];
                      }
                  }
                  return largest;
              }
          }
           

📸Output :

output