Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C++ Program to find Largest and Smallest Element in an Array

Largest and Smallest element in an array

Here we will discuss how to find the smallest element and largest element from inputted One Dimensional Array Elements. In this array we traverse elements recursively and encounter the value of smallest element and largest element until the end of the array with the help of concept of C++ and For loop in the code.

C++ Program to find Largest and Smallest Element in an Array

Algorithm:-

  1. Take the size of array from the user
  2. Input the array elements from the user
  3. Initialize small = large = arr[0]
  4. Repeat from i = 1 to size of array
  5. if(arr[i] > large)
  6. large = arr[i]
  7. if(arr[i] < small)
  8. small = arr[i]
  9. Print the element as smallest and largest

C++ Code:-

    
    //C++  Program to Find Largest and Smallest Element in an Array
    #include <iostream>
    int main()
        {
        int a[50], sizeilargesmall;

        cout<<“Enter the size of the array: “;
        cin>>size;

        cout<<“Enter the”<<size<<” elements of the array:\n;
        for(i = 0i < sizei++)
        cin>>a[i];

        large = a[0];
        for(i = 1i < sizei++)
        {
            if(large < a[i])   // if larger value is encountered
            {
                large = a[i]; // update the value of large
            }
        }
        cout<<“The largest element is: “<<large;

        small = a[0];  
        for(i = 1i < sizei++) 
        { 
            if(small>a[i])   // if smaller value is encountered
            {
                small = a[i];   // update the value of small
            }
        }
        cout<<\nThe smallest element is: “<< small;
        return 0;
        }
    
    Output:
    Enter the size of the array: 6
    Enter the 6 elements of the array:
    9
    32
    14
    75
    100
    55
    The largest element is: 100
    The smallest element is: 9

Important Codes related to Arrays

  • Find Smallest Element in an Array : C | C++ | Java | Python
  • Find Second Smallest Element in an Array : C | C++ | Java | Python
  • Find Largest element in an array : C | C++ | Java | Python
  • Find the Smallest and largest element in an array : C | C++ | Java | Python
  • Calculate the sum of elements in an array : C | C++ | Java | Python
  • Reverse an Array : C | C++ | Java | Python
  • Sort first half in ascending order and second half in descending : C | C++ | Java | Python
  • Sort the elements of an array : C | C++ | Java | Python
  • Finding the frequency of elements in an array : C | C++ | Java | Python
  • Finding the Longest Palindrome in an Array : C | C++ | Java| Python
  • Counting Distinct Elements in an Array : C | C++ | Java| Python
  • Finding  Repeating elements in an Array : C | C++ | Java | Python
  • Finding Non Repeating elements in an Array : C | C++ | Java | Python
  • Removing Duplicate elements from an array : C | C++ | Java
  • Finding Minimum scalar product of two vectors : C | C++ | Java | Python
  • Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
  • Counting the number of even and odd elements in an array : C | C++ | Java
  • Find all Symmetric pairs in an array : C | C++ | Java
  • Find maximum product sub-array in a given array : C | C++ | Java
  • Finding Arrays are disjoint or not : C | C++ | Java
  • Determine Array is a subset of another array or not : C | C++ | Java
  • Determine can all numbers of an array be made equal : C | C++ | Java
  • Finding Minimum sum of absolute difference of given array : C | C++ | Java
  • Sorting elements of an array by frequency : C | C++ | Java
  • Sort an array according to the order defined by another array : C | C++ | Java
  • Replace each element of the array by its rank in the array : C | C++ | Java
  • Finding equilibrium index of an array : C | C++ | Java| Python
  • Rotation of elements of array- left and right : C | C++ | Java| Python
  • Block swap algorithm for array rotation : C | C++ | Java| Python
  • Juggling algorithm for array rotation : C | C++ | Java | Python
  • Finding Circular rotation of an array by K positions : C | C++ | Java | Python

You can find the below Programs in all languages

array