Write a C++ program to find the largest and smallest element of an array
Program Code:
#include<iostream>
using namespace std;
int main()
{
int array[30];
int number;
int i, j,smallest,largest;
cout<<"Enter number of elements you want to insert in array : ";
cin>>number;
for(i=0;i<number;i++)
{
cout<<"Element "<<i+1<<":";
cin>>array[i];
cout<<endl;
}
smallest = array[0];
largest = array[0];
for(i=1;i<number;i++)
{
if(array[i] < smallest)
{
smallest = array[i];
}
if(array[i]>largest)
{
largest = array[i];
}
}
cout<<"Largest element is :"<<largest;
cout<<endl;
cout<<"Smallest element is :"<<smallest;
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter number of elements you want to insert in array : 5
Element 1: 11
Element 2: 22
Element 3: 33
Element 4: 44
Element 5: 55
Largest element is :55
Smallest element is :11
Thanks
Mukesh Rajput
Post A Comment:
0 comments: