- To find smallest number in a number array
User Input: array[ ] = {10,5,9,3,2,9,3}
Output: The smallest number in array is : 2
#include <iostream>
using namespace std;
int main() {
int arr[]={10,12,5,7,1,9,3,2,9,3};
int small;
int l= sizeof(arr)/sizeof(arr[0]);
small=arr[0];
for(int i=1; i<l; i++){
if(small>arr[i])
small=arr[i];
}
cout<<"The smallest number in array is : ";
cout<<small;
return 0;
}
The smallest number in array is : 1
We are pre-defining the array in here, you can customize your array
ProgramMain Logic :
//To find size of an array
int l= sizeof(arr)/sizeof(arr[0]);
//take 1st element of an array as small(ASSUME)
small=arr[0];
//finding the smallest
for(int i=1; i<l; i++){
if(small>arr[i])
small=arr[i];
}
//Finally print the 'small' value
To find the largest one, just change if(small>arr[i])
to if(small<arr[i])
Coming Soon !
QuickPractice isn’t the thing you do once you’re good. It’s the thing you do that makes you good.