This article contains C++
Program to Find Largest Element in Array.
To understand this program, you need to
know
(1) C++ Looping
(2) C++ if...else
C++ Program to Find Largest Element in
Array
#include<iostream>
using namespace std;
int main()
{
int
l,arr[50],n,i;
cout<<"Enter
the size of array (under 50) : ";
cin>>n;
for(i=0;
i<n; i++)
{
cout<<"\nEnter array element "<<i+1<<" :
";
cin>>arr[i];
}
l=arr[0];
for(i=0;
i<n; i++)
{
if(l<arr[i])
{
l=arr[i];
}
}
cout<<"\nLargest
Number : "<<l;
return
0;
}
OUTPUT:
Enter the size of array (under 50) : 5
Enter array element 1 : 9
Enter array element 2 : 4
Enter array element 3 : 3
Enter array element 4 : 5
Enter array element 5 : 8
Largest Number : 9
C++ Program to Find Largest Element in
Array Using Functions
#include<iostream>
using namespace std;
void largest(int [], int);
int main()
{
int
arr[50],n,i;
cout<<"Enter
the size of array (under 50) : ";
cin>>n;
for(i=0;
i<n; i++)
{
cout<<"\nEnter array element "<<i+1<<" :
";
cin>>arr[i];
}
largest(arr,
n);
return
0;
}
void largest(int a[], int n)
{
int l=a[0],i;
for(i=0;
i<n; i++)
{
if(l<a[i])
{
l=a[i];
}
}
cout<<"\nLargest
Number : "<<l;
}
OUTPUT:
Enter the size of array (under 50) : 5
Enter array element 1 : 58
Enter array element 2 : 49
Enter array element 3 : 65
Enter array element 4 : 29
Enter array element 5 : 38
Largest Number : 65
0 comments:
Post a Comment