C, C++ Program to Print Smallest Element of an Array |

To understand this problem, Let's take an array

**arr[] = {3, 4, 1, 5, 6}.**The smallest number in an array is 1. Now the question is how to solve this problem. You can use multiple approaches to solve this problem.

**METHOD 1**: Use Sorting Algorithm.

You can sort an array. After sorting an array pick the first element of an array, now the element at first position is smallest element of an array.

For sorting, You can use sorting algorithm such as Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, Merge Sort etc.

You can compare sorting algorithm and their time complexity before writing a code.

**METHOD 2**: By Traversing an Array.

To find a smallest element using this method, take a variable and assign the first element of an array.

###
int smallest = arr[0];

Now traverse an array and check if any other element is smaller than the assign one. If it is smaller, then replace the variable to a new value.

/* Traversing an array. */
for( i=0;i < size of an array;i++){
/* Next element is greater than the current element,
assign them to smallest variable. */
if(smallest > arr[i]){
smallest = arr[i];
}
}

int smallest = arr[0];

Now traverse an array and check if any other element is smaller than the assign one. If it is smaller, then replace the variable to a new value.

/* Traversing an array. */ for( i=0;i < size of an array;i++){ /* Next element is greater than the current element, assign them to smallest variable. */ if(smallest > arr[i]){ smallest = arr[i]; } }

## C Program to Print Smallest Number of an Array

#include <stdio.h> int main(void) { int arr[100], n, i, smallest; printf("Enter the size of an array \n"); scanf("%d", &n); printf("Enter an elements in an array\n"); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Assign first element of an array smallest = arr[0]; // Traverse an array for(i = 0; i < n; i++) { /* If any value is smaller than the current value */ if(smallest > arr[i]) { smallest = arr[i]; } } printf("The smallest element of an array is %d ", smallest); return 0; }

## C++ Program to Print Smallest Number of Array

#include <iostream> using namespace std; int main() { int arr[100], n, i, smallest; cout << "Enter the size of an array \n"; cin >> n; cout << "Enter an elements in an array \n"; for (i=0; i<n; i++) { cin >> arr[i]; } /* Assign first element of an array. */ smallest = arr[0]; for(i = 0; i < n; i++) { if(smallest > arr[i]) { smallest = arr[i]; } } cout << "The Smallest element of an array is " << smallest; return 0; }

**Output :**

**Enter the size of an array : 5**

Enter an elements of an array : 8 2 3 1 6

The smallest element of an array is 1

Programming question on Arrays

Programming question on Linked List

Sorting algorithm and their time complexity

Stack implementation

Programming questions using Recursion

C, C++ interview questions

Programming Books

If you know some other methods for solving this problem, you can let us know through your comments.

## 0 comments:

## Post a Comment