Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to find smallest number of an array.

C Code Example — Array Programs

ADVERTISEMENT

C Program to find smallest number of an array.

Objective

Write a C program to find the smallest number in an array.

Algorithm / Approach

  1. Read 5 elements into an array a[5].
  2. Assume the very first element is the smallest: min = a[0].
  3. Loop through the remaining elements.
  4. If an element is strictly less than the current minimum, update it: min = a[i+1].
  5. Print the minimum value.
main.c
#include<stdio.h>
int main( ) {
 int i,min;
 int a[5];
 printf("Enter 5 elements : ");
 for(i=0; i < 5; i++) {
  scanf("%d",&a[i]);
 }
 min = a[0];
 for(i=0; i < 5-1; i++) {
  if( a[i+1] < a[i] )
   min = a[i+1];
 }
 printf("Smallest Number = %d",min);
 return 0;
}

Expected Output

Enter 5 elements : 10 12 15 23 20
Smallest Number = 10

Explanation of the Program

  • This logic is identical to finding the maximum, just with the comparison operator flipped from &gt; to &lt;.
  • Note on the provided code: the loop uses a[i+1] while i goes up to 3 (since i &lt; 5-1). This correctly checks indices 1 through 4 against the baseline, but the standard way is to loop i from 1 to 4 and just check a[i].

Complexity

Time Complexity O(n)
Space Complexity O(n)
ADVERTISEMENT