JAVA SP T


Lets dive into snapshot of my programs!💻

20.Program to Find Smallest Number in an array

💡Code:

  
                public class SmallestNumber7078 {
                    public static void main(String[] args) {
                        // Example array
                        int[] array = {5, 2, 8, 1, 9, 3, 6};
                
                        // Display the original array
                        System.out.println("Original Array:");
                        displayArray(array);
                
                        // Find the smallest number
                        int smallest = findSmallest(array);
                
                        // Display the result
                        System.out.println("\nSmallest Number: " + smallest);
                    }
                
                    // Method to find the smallest number in an array
                    private static int findSmallest(int[] arr) {
                        int smallest = Integer.MAX_VALUE;
                
                        for (int value : arr) {
                            if (value < smallest) {
                                smallest = value;
                            }
                        }
                
                        return smallest;
                    }
                
                    // Method to display the elements of an array
                    private static void displayArray(int[] arr) {
                        for (int value : arr) {
                            System.out.print(value + " ");
                        }
                        System.out.println();
                    }
                }
                

               

📸Output :

output