Java Program To Find Largest And Smallest Number In An Array

Sharing is healthy !!

Hey guys!!! Welcome to flower brackets blog. In today’s post let us learn java program to find largest and smallest number in an array.

Here, i’m gonna show you how to find the smallest number in an array java using Scanner class and without using Scanner class.

Let’s see find max value in array java,

Java Program To Find Largest And Smallest Number In An Array

JAVA PROGRAM TO FIND LARGEST AND SMALLEST NUMBER IN AN ARRAY WITHOUT USING SCANNER CLASS

Also Read – Convert Decimal to Octal Java Program

public class LargestSmallest
{
   public static void main(String[] args)
   {
      // assigning array of 10 numbers to numerals
      int numerals[] = new int[]{32,43,53,54,32,65,63,98,43,23};

      // assigning first numeral of an array to largest and smallest

      int largest = numerals[0];
      int smallest = numerals[0];

      for(int x = 1;x < numerals.length;x++)
      {
         if(numerals[x] > largest)
            largest = numerals[x];
         else if(numerals[x] < smallest)
            smallest = numerals[x];
      }
      System.out.println("Largest Number is: " + largest);
      System.out.println("Smallest Number is: " + smallest);
   }
}

Output:

Largest Number is: 98
Smallest Number is: 23


JAVA PROGRAM TO FIND LARGEST AND SMALLEST NUMBER IN AN ARRAY USING SCANNER CLASS

Also Read – Java Program To Find Even Or Odd

import java.util.Scanner;

public class LargestSmallest
{
   public static void main(String[] args)
   {
      // assigning array of 10 numbers to numerals

      int a;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter number of elements to find largest and smallest number: ");
      a = sc.nextInt();
      int numerals[] = new int[a];
      System.out.println("Enter " + a + " integers.");
      
      for(int b = 0;b < a;b++)
         numerals[b] = sc.nextInt();

      // assigning first numeral of an array to largest and smallest

      int largest = numerals[0];
      int smallest = numerals[0];

      for(int x = 1;x < numerals.length;x++)
      {
         if(numerals[x] > largest)
            largest = numerals[x];
         else if(numerals[x] < smallest)
            smallest = numerals[x];
      }
      System.out.println("Largest Number is: " + largest);
      System.out.println("Smallest Number is: " + smallest);
   }
}

Output:

Enter number of elements to find largest and smallest number: 10
Enter 10 integers. 23 34 90 34 31 67 89 12 40 87
Largest Number is: 90
Smallest Number is: 12


find max value in array java

Let’s learn how to find max element in array java with an example,

import java.util.Scanner;

public class MaxValue
{
   public static void main(String[] args)
   {
      int num, maxValue;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter number of elements in the array : ");
      num = sc.nextInt();
      int arr[] = new int[num];
      System.out.println("Please enter elements of array : ");
      for(int a = 0; a < num; a++)
      {
         arr[a] = sc.nextInt();
      }
      maxValue = arr[0];
      for(int a = 0; a < num; a++)
      {
         if(maxValue < arr[a])
         {
            maxValue = arr[a];
         }
      }
      System.out.println("Max of array in java : " + maxValue);
      sc.close();
   }
}

Output:

Please enter number of elements in the array : 5
Please enter elements of array :
5
8
7
10
1
Max of array in java : 10


conclusion

That’s it guys. This is all about how to find max element in array java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Sharing is healthy !!