JAVA SP T


Lets dive into snapshot of my programs!💻

10. Program to print the smallest element in an arra

💡Code:

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

📸Output :

output