In this tutorial, we will discuss the small concept of Cpp program to find the largest elements of an Array.
In this topic, we will learn how to find the largest elements of array elements in Cpp programming language
This program gets “n” number of elements and Enter the elements of the array as input from the user. then, this program finds and displays the largest elements from the array using for loops.
In this programs, we can see step by step procedure for completion of the program.
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[30];//array declaration int length,i, max; //essential variable declaration cout<<"Enter the length of the array: "; cin>>length;//get input from user for length for(i=0; i<length; i++){ cout<<"Enter element "<<i+1<<" :";//enter elements for array cin>>arr[i]; } max=arr[0]; for(i=0; i<length; i++){ if(max<arr[i]){ max=arr[i]; } } cout<<"\nlargest elements is:"<<max; getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the length of the array: 5 Enter element 1:87 Enter element 1:65 Enter element 1:78 Enter element 1:43 Enter element 1:56 largest element si: 87
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { float array[100];//Array declaration int length,i; double max;//variable declaration cout << "Enter the length of the array: " << endl; cin>>length;//Takes input from user for length for(i=0; i<length; i++){ cout<<"Enter element "<<i+1<<" :";//enter elements for array cin>>array[i]; } max=array[0]; for(i=0; i<=length; i++){ if(max<array[i]){ max=array[i]; } } cout<<"\n Largest elements is: "<<max; getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the length of the array: 5 Enter element 1 :4.6 Enter element 1 :3.7 Enter element 1 :7.9 Enter element 1 :9.8 Enter element 1 :3.2 Largest elements is: 9.8
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…