Q. Write a program to find smallest element in an array.



For Example :-
input : arr[]={ 8, 9, 1, 2, 4 }
output : 1

So as we can see that the smallest element in arr[] = {8, 9, 1, 2, 4 } is 1 so the output is 1.

Smallest element in an array algorithm

START
   Step 1 → Take an array Arr and define its values
   Step 2 → Declare one variable (VAR) and assign 1st element of array to it
   Step 3 → Loop for each value of Arr Repeat Step 4
   Step 4 → If Arr[I] < VAR, Assign Arr[I] to VAR
   Step 5 → After loop finishes, Display VAR which holds the smallest element of array
STOP


Smallest element in an array program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main() {
        int i, arr[5]={ 7, 9, 1, -15, 0};
    	int VAR = arr[0];
        
        for (i = 1; i < 5; ++i) {
            if (VAR > arr[i])
                VAR = arr[i];
        }
    
        printf("Smallest element in an Array is = %d", VAR);
    
        return 0;
    }
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
           int i, arr[5]={ 7, 9, 1, -15, 0};
    	   int VAR = arr[0];
       
        for(i = 1;i < 5; ++i)
        {
           if(VAR > arr[i])
               VAR = arr[i];
        }
        cout << "Smallest element in an Array is = " << VAR;
    
        return 0;
    }
    
    public class LFC {
    
        public static void main(String[] args) {
            int[] numArray = { 7, 9, 1, -15, 0};
            int smallest = numArray[0];
    
            for (int num: numArray) {
                if(smallest > num)
                    smallest = num;
            }
    
            System.out.format("Smallest element in an Array is = %d", smallest);
        }
    }
    
    number = [7, 9, 1, -15, 0]
    smallest_number = min(number);
    
    print("Smallest element in an Array is = ", smallest_number)
    
    using System;
    
        class LFC
        {
            static void Main()
            {
                int i       = 0;
                int small   = 0;
          //array declaration
                int[] arr = {7, 9, 1, -15, 0};
          
          //reading array elements 
              
                //assigning first element to the array
          small = arr[0];
          //loop to compare value of small with other elements
                for (i = 1; i < 5; i++)
                {
            //if small is smaller than other element
            //assig that element to the small
                    if (small > arr[i])
                        small = arr[i];
                }
          //finally, we will have smallest element, printing here
                Console.WriteLine("Smallest element in an Array : " + small);
            }
        }
    
    
    
    // Returns maximum in array 
    function getMin($arr)  
    { 
       $n = count($arr);  
       $min = $arr[0]; 
       for ($i = 1; $i < $n; $i++)  
           if ($min > $arr[$i]) 
               $min = $arr[$i]; 
        return $min;        
    } 
    
      
    
      
    // Driver code 
    $arr = array(7, 9, 1, -15, 0); 
    echo "Smallest element in an Array : ";
    echo(getMin($arr)); 
    
    
    #include <stdio.h>
    int main() {
        int i, arr[5]={ 7, 9, 1, -15, 0};
    	int VAR = arr[0];
        
        for (i = 1; i < 5; ++i) {
            if (VAR > arr[i])
                VAR = arr[i];
        }
    
        printf("Smallest element in an Array is = %d", VAR);
    
        return 0;
    }
    

    Output

    Smallest element in an Array is = -15
    

    Recommended Programs

       Program to find largest element in an array
       Program to check leap year