Write a Java program to find the largest and smallest number in an array.

Here’s a Java program to find the largest and smallest number in an array:

import java.util.Scanner;

public class ArrayMinMax {
    public static void main(String[] args) {
        int[] arr = new int[5];
        int min, max;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter 5 numbers: ");
        for (int i = 0; i < 5; i++) {
            arr[i] = scanner.nextInt();
        }
        min = max = arr[0];
        for (int i = 1; i < 5; i++) {
            if (arr[i] < min) {
                min = arr[i];
            } else if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println("Minimum number in the array: " + min);
        System.out.println("Maximum number in the array: " + max);
    }
}

In this program, we first create an integer array of size 5 and take user input for its elements using a for loop. We then initialize two variables `min` and `max` to the first element of the array.

We then use another for loop to compare each element of the array with `min` and `max` and update their values accordingly. If an element is less than `min`, then we update `min` to that element. If an element is greater than `max`, then we update `max` to that element.

At the end of the program, we print out the values of `min` and `max`, which represent the smallest and largest numbers in the array, respectively.