Java Program To Find Smallest and Largest Number in an Array
Java Programming

Java Program To Find Smallest and Largest  Number in an Array

 

Find 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);

                }

                }

Find the Smallest and Largest number in the array of Java

                Explanation ….

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

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment