In this 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-
C++ program to print the smallest element in an array
C++ program to print the smallest element in an array |
sample input
enter the range - 4
2 4 6 3
The smallest element is
2
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 small element in the array , Take a variable for comparing the small element,.
- Then check inside the if the condition that the value that is smaller than that assign it to small and print the small value
- Then again run a for loop to displaying the array after reversing.
Source Code
#include<iostream>
using namespace std;
int main()
{
int n,small;
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(small>arr[i])
small=arr[i];
}
cout<<"The smallest elemnt in the array is :"<<small;
return 0;
}
Solve by using the function
Source Code
// Write a C++ program to find the smallest element of a given array of integers
#include <iostream>
using namespace std;
void small(int arr[], int n)
{
int smalll;
for (int i = 0; i < n; i++)
{
if (smalll < arr[i])
smalll = arr[i];
}
cout << "The smallest element in the array is :" << smalll;
}
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];
}
small(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
FEEDBACK
If still you have any doubts please comment below and please share your approaches with us .
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 with us .
Post a Comment
Post a Comment
Please do not spam in comment