Write a program in C++ to Display Largest Element in the array.




Program Code:
#include <iostream> 
using namespace std;
int main() 
{    
int j, n;    
int array[20];    
cout << "Enter number of elements in the given array ";   
cin >> n;
cout<<endl;    
for( j = 0; j < n;  j++) // Loop to store number in the array  
{     
cout << "Enter Number is " << j + 1 << " : ";      
cin >> array[j];
cout<<endl;
}    
for(j = 1 ; j < n; j++) // Loop to store largest number at first position   
{         
if(array[0] < array[j])
array[0] = array[j]; 
}
}
cout << "Largest element in the array is = " << array[0];



return 0;
}



Output of the above program: 




Enter number of elements in the given array 

Enter Number is 1 : 2
Enter Number is 2 : 3
Enter Number is 3 : 5
Enter Number is 4 : 6
Enter Number is 5 : 4

Largest element in the array is = 6






Note: This program is tested on www.jdoodle.com under C++ language compiler link.




Thanks

Mukesh Rajput




Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: