Java Examples

100+ tutorials, source code, and examples to help you develop creative and technical skills.

Java Program to find smallest and largest elements in Array from User Input

FindSmallest.java

/* Code From Learnoset - Learn Code Online Android App.*/
/*Download Learnoset App from PlayStore to learn coding*/

/*Java Program to find smallest and largest elements in Array from User Input*/

import java.util.Scanner;

public class FindSmallest {
    public static void main(String[] args) {

        //Declare int type array to store user entered values; 
        int[] numbers;

        Scanner scanner = new Scanner(System.in); // Create an object of Scanner Class

        System.out.println("Enter size of array");
        int sizeOfArray = scanner.nextInt(); // get how many elements user want to store in numbers array;

        numbers = new int[sizeOfArray]; // set size of array to store elements from user input

        System.out.println("Put elements in array");
        for (int i = 0; i < sizeOfArray; i++) {

            // read array element until it reaches the size of array
            int arrayElement = scanner.nextInt();
            numbers[i] = arrayElement;
        }

        // Print all elements of numbers array 
        System.out.println("all elements of array:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + ", ");
        }

        //Initialize smallest with first element of array.  
        int smallest = numbers[0];

        //Loop through the array  
        for (int i = 0; i < numbers.length; i++) {
            //Compare elements of array with smallest  
            if (numbers[i] < smallest) {
                smallest = numbers[i];
            }

        }

        System.out.println("\nSmallest element present in given array: " + smallest);
    }
}

/*----------OUTPUT----------
Enter size of array
5
Put elements in array
1
5
6
8
9
all elements of array:
1, 5, 6, 8, 9,
Smallest element present in given array: 1
*/

FindLargest.java

/* Code From Learnoset - Learn Code Online Android App.*/
/*Download Learnoset App from PlayStore to learn coding*/

//Java Program to find smallest and largest elements in Array from User Input
import java.util.Scanner;

public class FindLargest {
    public static void main(String[] args) {

        //Declare int type array to store user entered values; 
        int[] numbers;

        Scanner scanner = new Scanner(System.in); // Create an object of Scanner Class

        System.out.println("Enter size of array");
        int sizeOfArray = scanner.nextInt(); // get how many elements user want to store in numbers array;

        numbers = new int[sizeOfArray]; // set size of array to store elements from user input

        System.out.println("Put elements in array");
        for (int i = 0; i < sizeOfArray; i++) {

            // read array element until it reaches the size of array
            int arrayElement = scanner.nextInt();
            numbers[i] = arrayElement;
        }

        // Print all elements of numbers array 
        System.out.println("all elements of array:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + ", ");
        }

        //Initialize largest with first element of array.  
        int largest = numbers[0];

        //Loop through the array  
        for (int i = 0; i < numbers.length; i++) {
            //Compare elements of array with smallest  
            if (numbers[i] > largest) {
                largest = numbers[i];
            }

        }

        System.out.println("\nLargest element present in given array: " + largest);
    }
}

/*----------OUTPUT----------
Enter size of array
5
Put elements in array
1
2
6
5
9
all elements of array:
1, 2, 6, 5, 9,
Largest element present in given array: 9
*/

Projects with Source Code + Video Tutorials

You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

Login-&-Register-Screen-UI-Design---02
Login & Register Screen UI Design - 02
Modern-Splash-Screen-with-Animations---01
Modern Splash Screen with Animations - 01
How-to-create-Modern-Login-and-Register-screen-in-Android-Studio-|-Material-UI-Design-|-Part---06
How to create Modern Login and Register screen in Android Studio | Material UI Design | Part - 06

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.