Find Maximum & Minimum Element in an Array Using C++

Hello Learners,

In this article, we’ll be learning how to Find the Maximum and Minimum elements of an Array in C++. The array can either be user-defined or by default given in the program. Loops are going to be used in this program but we can also do it in another method which is Recursion.
Let us see an example to understand properly:
Suppose an array is given like:
here, the maximum element is 100 and the minimum element is 20. We will write a program to print this on the output screen.

C++ Program to find Maximum & Minimum Element of an Array

let us consider an array named arr[n]. Here, n is the size of the array which should be an integer. we will consider here only 5 elements but you can choose your array size as per your choice. We will use a loop to solve this problem. This is also called the iterative approach. Firstly, we will put the first element in two variables named max and min. Then, we’ll compare every element of the array with these variables and if found the maximum and minimum then put that particular element in those variables max and min respectively. Lastly, we will print the value of max and min as the maximum element and minimum element.

Now, let us see the source code for a better understanding:

using namespace std;

int main()
  int arr[5],max,min,i;
  cout<<"Enter elements of array: ";

  cout<<"Your array is: ";
  cout<<arr[i]<<"  ";

  else if(min>arr[i])

  cout<<"\nMaximum element of Array: "<<max;
  cout<<"\nMinimum element of Array: "<<min;
  return 0;

Firstly, in max and min, we will put the first element of the array. And, variable i is used for controlling the loop where all the elements are being compared with variable max and min.

Let us see the output now:

Enter elements of array: 100
Your array is: 100 30 45 2 78
Maximum element of Array: 100
Minimum element of Array: 2

In this way, you can find the Maximum and Minimum elements of an array. I hope it was easy enough to understand. If you have any doubt, feel free to ask in the comment section.


Isha Rani Prasad
Codespeedy Tech Pvt. Ltd.

Leave a Reply

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