Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// find largets element from array
#include<iostream>
#define clrscr() system("cls")
using namespace std;
int main()
{
int ar[100],i,n,max,p;
clrscr();
xyz:cout<<"Enter the limit\n";
cin>>n;
if(n>100)
{
cout<<"\nInvalid limit pls enter 0 to 100";
goto xyz;
}
else
{
cout<<"\nEnter elements in array\n";
for(i=0;i<n;i++)
cin>>ar[i];
max=ar[0];
p=0;
for(i=1;i<n;i++)
{
if(ar[i]>max)
{
max=ar[i];
p=i;
}
}
cout<<"\n\n***Largets element is*** : "<<max;
cout<<"\n\n***Position is *** : "<<++p;
}
}
Program 2
// find smallest element from array
#include<iostream>
#define clrscr() system("cls")
using namespace std;
int main()
{
int ar[100],i,n,min,p;
clrscr();
xyz:cout<<"Enter the limit\n";
cin>>n;
if(n>100)
{
cout<<"\nInvalid limit pls enter 0 to 100";
goto xyz;
}
else
{
cout<<"\nEnter elements in array\n";
for(i=0;i<n;i++)
cin>>ar[i];
min=ar[0];
p=0;
for(i=1;i<n;i++)
{
if(ar[i]<min)
{
min=ar[i];
p=i;
}
}
cout<<"\n\n***Smallest element is*** : "<<min;
cout<<"\n\n***Position is *** : "<<++p;
}
}