Java Program To Find Smallest and Largest  Number in an Array

Java Program To Find the Smallest and Largest Numbers in an Array

Find the Largest and Smallest Number in an Array means how to find the largest and smallest numbers in an array. In this example, we use 10 numbers in which we find the smallest and largest value.

Program to Find Smallest and Largest in Java Array

public class FindLargeSmall

 {

public static void main(String[] args)

{

//array of 10 numbers

          int numbs[] = new int[]{32,43,53,54,32,65,63,98,43,23}; 

          //assign the first element of an array to the largest and smallest

          int smallestno = numbs[0];                         

//assign the first element of an array to the smallest

          int largetstno = numbs[0];                            

//assign the first element of an array to the largest

            //for loop is used to find the largest and smallest from start to end digit

              for(int i=1; i< numbs.length; i++)  

                {

                if(numbs[i] > largetstno)                     //condition check if true then

                largetstno = numbs[i];                        //assign the value to largestst

               else if (numbs[i] < smallestno)   

                smallestno = numbs[i];

                 }

                 System.out.println(“Largest Number is : ” + largetstno);

                System.out.println(“Smallest Number is : ” + smallestno);

                }

                }

Java Program To Find Smallest and Largest  Number in an Array
Java Program To Find the Smallest and Largest Numbers in an Array
Find the Smallest and Largest numbers in the array of Java

                Explanation ….

In the above program, we use an array to find the smallest and largest numbers. We enter 10 numbers in an array, and by using a for loop, we can find the result. Searching always starts with zero. The index of the elements in an array always starts with 0.

Program to Find Smallest and Largest in Java Array using Scanner class.

import java.util.Scanner;

public class SmallestLargestInArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input array size
System.out.print(“Enter number of elements: “);
int n = sc.nextInt();

int[] arr = new int[n];

// Input array elements
System.out.println(“Enter ” + n + ” numbers:”);
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

// Initialize smallest and largest
int smallest = arr[0];
int largest = arr[0];

// Traverse array
for (int i = 1; i < n; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
if (arr[i] > largest) {
largest = arr[i];
}
}

// Output result
System.out.println(“Smallest number: ” + smallest);
System.out.println(“Largest number: ” + largest);

sc.close();
}
}

Program to Find Smallest and Largest in Java Array using Scanner class.
Program to Find Smallest and Largest in Java Array using Scanner class.

Java program to convert Fahrenheit scale to Celsius

Java Program To Swap Two Numbers