Program to Find the Largest & Smallest Element in an Array [C, C++, Python & Java]

Posted in

Program to Find the Largest & Smallest Element in an Array [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on December 27, 2024

    This tutorial will help you learn to write a program in C, C++, Python, and Java to find the largest and smallest element from an array.

    To write the program, you must know the concept of the array and the way to use it, the programming language syntax, and the for loop.

    So, let us get started!

    What is an Array?

    An array is a data structure that stores a collection of items of the same data type at contiguous memory locations. It is the simplest data structure where you can access each element using its index number.

    When you need to store a large amount of data of a similar type, an array comes in handy. For instance, if you want to store the marks of a student scored in all subjects, you can use an array instead of using a separate variable for each subject mark.

    C Program to Find the Largest and Smallest Elements in an Array

    #include<stdio.h>
    int main()
    {
        int arr[100],num, elements, min, max;
        printf("How many elements do you want to enter an array? "); 
        scanf("%d", &num);
        printf("Enter the elements in the array\n");
        for(int i=0; i<num;i++)
        {
            scanf("%d", &arr[i]);
            max=arr[0];
            min=arr[0];
        }
        for(int i=0; i<num; i++)
        {
            if(arr[i]>max)
            max= arr[i];
            if(arr[i]<min)
            min= arr[i];
        }
        printf("The largest Element is: %d \n", max);
        printf("The smallest Element is: %d", min);
        return 0;
    }

    Output:

    How many elements do you want to enter an array? 6
    Enter the elements in the array
    34
    56
    76
    78
    100
    345
    The largest Element is: 345 
    The smallest Element is: 34

    C++ Program to Find the Largest and Smallest Elements in an Array

    Here you can take the below-listed program. If you want to find the largest and smallest elements from an array.

    #include<iostream>
    using namespace std;
    int main()
    {
        int arr[100],num, elements, min, max;
        cout<<"How many elements do you want to enter an array? "; 
        cin>>num;
        cout<<"Enter the elements in the array\n";
        for(int i=0; i<num;i++)
        {
            cin>>arr[i];
            max=arr[0];
            min=arr[0];
        }
        for(int i=0; i<num; i++)
        {
            if(arr[i]>max)
            max= arr[i];
            if(arr[i]<min)
            min= arr[i];
        }
        cout<<"The largest Element is: "<<max<<endl;
        cout<<"The smallest Element is: "<<min<<endl;
        return 0;
    }

    Output:

    How many elements do you want to enter an array? 5
    Enter the elements in the array
    34
    23
    46
    87
    99
    The largest Element is: 99
    The smallest Element is: 23

    Python Program to Find the Largest and Smallest Elements in an Array

    arr=[]
    num = int(input("How many elements do you want to enter in an array? "))
    print("Enter the elements in the array")
    for i in range(num):
        element = int(input())
        arr.append(element)
    print("The largest Element is:",max(arr))
    print("The smallest Element is:",min(arr))

    Output:

    How many elements do you want to enter in an array? 7
    Enter the elements in the array
    
    20
    234
    2345
    23
    55
    345
    656
    
    The largest Element is: 2345
    The smallest Element is: 20

    Java Program to Find the Largest and Smallest Elements in an Array

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            int num, min, max, i;
            int arr[] = new int[100];
            Scanner sc = new Scanner(System.in);
            System.out.println("How many elements do you want to enter in the array?");
            num = sc.nextInt();
            
            System.out.println("Enter the elements in the array:");
            for (i = 0; i < num; i++) {
                arr[i] = sc.nextInt();
            }
            
            // Initialize min and max with the first element of the array
            min = arr[0];
            max = arr[0];
            
            for (i = 1; i < num; i++) {
                if (arr[i] > max)
                    max = arr[i];
                
                if (arr[i] < min)
                    min = arr[i];
            }
            
            System.out.println("The largest element is: " + max);
            System.out.println("The smallest element is: " + min);
        }
    }

    Output:

    How many elements do you want to enter in the array?
    5
    Enter the elements in the array:
    23
    45
    389
    456
    1234
    The largest element is: 1234
    The smallest element is: 23

    Conclusion

    This was all about writing a program to find out the largest & smallest element in an array. We wrote a program in C, C++, Python, and Java that asks the end-user about the number of elements in an array and enter those elements. After that, the program itself finds out the largest and smallest element in an array.

    If you have any doubts regarding this article, do let us know in the comments.

    People are also reading:

    Leave a Comment on this Post

    0 Comments