Find the Smallest and Largest Number in Array in C/C++, Java and Python

Find the Smallest and Largest Number in Array in C/C++, Java and Python

Here is the program to find the smallest and largest number in the array in C/C++, Java and Python.

I have also added the comment explaining each line of the code. So the code is self explanatory.

C/C++ Program

Prerequisites:

C/C++ Code:

#include <stdio.h>
 
int main() {
    //initialize array
    int arr[7] = {34, 62, 56, 13, 7, 9, 17};
    int i=0;

    //initialize first number as smallest number
    int small = arr[0];

    //initialize first number as largest number
    int large = arr[0];
 
    // traverse all number fomr the array
    for(i = 0; i < 7; i++) {

         //compare smallest number with current number
        if(arr[i]<small)
            small = arr[i];

        //compare largest number with current number
        if(arr[i]>large)
            large = arr[i];
        
    }
 
    //print smallest number from array
    printf("Smallest element in array: %d", small);

    //print largest number from array
    printf("\nLargest element in array: %d", large);

    return 0;
}

Output:

Smallest element in array: 7 
Largest element in array: 62

This is the C program. This program also work with C++ compiler.

Java Program

Prerequisites:

Java Code:

public class Smallest_Largest_Element_Array {  
    public static void main(String[] args) {  
  
        // initialize the array  
        int [] arr = new int [] {67, 83, 8, 71, 29, 3, 48};  

        //Initialize smallest and largest element as first element  
        int small = arr[0]; 
        int large = arr[0]; 

        //traverse all elements in the array  
        for (int i = 0; i < arr.length; i++) {  

            //compare smallest number with current number  
            if(arr[i] < small)  
               small = arr[i]; 

            //compare largest number with current number  
            if(arr[i] > large)  
               large = arr[i]; 
        }

        //print smallest number from the array
        System.out.println("Smallest element in the array: " + small);

        //print largest number from the array
        System.out.println("Largest element in the array: " + large);
    }  
}  

Output:

Smallest element in the array: 3
Largest element in the array: 83

Python Program

Prerequisites:

Python Code:

#initialize the Python list
int_arr = [45, 56, 83, 4, 7, 28, 36]

#initialize first number as the smallest number
small = int_arr[0]
#initialize first number as the largest number
large = int_arr[0]

#loop over all the number in the list
for i in int_arr:
    #compare smallest number with current number
    if i<small:
        small=i

    #compare largest number with current number
    if i>large:
        large=i

print(f"Smallest number in the Python list: {small}")
print(f"Largest number in the Python list: {large}")

Output:

Smallest number in the Python list: 4 
Largest number in the Python list: 83

We can also use the min() and max() method to find the smallest (minimum) and largest (maximum) number from the Python list.

Complexity

We are traversing elements from the array only once. So the time complexity of the algorithm is O(n) where n is the size of the array.

Hope this tutorial about finding smallest and largest number in the array helps you.

Leave a Reply

Your email address will not be published. Required fields are marked *