In this article we discussed about how to find the smallest element in the array with some approaches and you can also do with more approaches
the below points mentions we discussed in this article
- Question
- sample input
- Logic
- solution with loop
- solution using function
- Feedback
Question-
Write a C++ Program to find the biggest element in the array
sample input
enter the range - 4
2 4 6 3
The biggest element is
6
C++ Program to find largest element in array |
Logic to solve the problem
- Take an array of some integers then take a variable ( n ) for taking the input how many data the user want to enter then run a for loop to taking the inputs .
- Again run another for loop and inside the loop use if condition for checking the large element in the array , Take a variable and initialize it from =0,.
- Then check inside the if the condition that the value that is greater than large assign it to large and print the large value
- Then again run a for loop to displaying the array after reversing.
Source Code
#include<iostream>
using namespace std;
int main()
{
int n,large=0;
cout<<"Enter the size :"<<endl;
cin>>n;
int arr[100];
for(int i=0;i<n;i++)
{
cout<<"Enter the element no."<<i+1<<" :";
cin>>arr[i];
}
for(int i=0;i<n;i++)
{
if(large<arr[i])
large=arr[i];
//here we define from first that any element greater than zero in the array will become the greater array
//then we declare as it is greater
//in the next step if any value found in the array that is greater than the value store in array then it will assign to
//large and declare as bigger value and the process continue till the end
}
cout<<"The biggest elemnt in the array is :"<<large;
return 0;
}
Solve by using the function
Source Code
// Write a C++ program to find the bigest element of a given array of integers
#include <iostream>
using namespace std;
void large(int arr[], int n)
{
int lrg=0;
for (int i = 0; i < n; i++)
{
if (lrg < arr[i])
lrg = arr[i];
}
cout << "The biggest elemnt in the array is :" << lrg;
}
int main()
{
int n;
cout << "Enter the size :" << endl;
cin >> n;
int arr[100];
for (int i = 0; i < n; i++)
{
cout << "Enter the element no." << i + 1 << " :";
cin >> arr[i];
}
large(arr, n);
return 0;
}
Check Out OUR Home page for more information about practice problem
Also check Out
Also check Out Projects for Resume
Problems On C++
Q-2) Vectors In c++
Problems on TCS paper
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
FEEDBACK
If still you have any doubts please comment below and please share your approaches in the comment to know others and help others.
If still you have any doubts please comment below and please share your approaches in the comment to know others and help others.
Post a Comment
Post a Comment
Please do not spam in comment