C++ Program to find minimum element in given array














































C++ Program to find minimum element in given array



Program description :

 In this article , we will see how to find smallest element in given array in linear time.


C++ Program to find minimum element in given array

#include <bits/stdc++.h> using namespace std ; int main() { int n , i; cout<<"Enter the number of elements in array : \n"; cin>>n; //declaring array of size n int arr[n];
cout<<"Enter the elements of array : \n"; for(i=0 ; i < n ; i++) { cin>>arr[i]; } //assuming first element is smallest element int min = arr[0]; //comparing every element with minimum element for(i=1 ; i<n ; i++) { if( arr[i] < min ) { min = arr[i]; } } cout<<"Minimum element in given array is "<< min << endl; }

Output :

Enter the number of elements in array :
5
Enter the elements of array :
5
14
2
20
18
Minimum element in given array is 2




Comments