C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Minimum in array

C Program: Minimum in array

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i, min;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Assume first element as minimum

    min = arr[0];

 

    // Find minimum element using for loop

    for(i = 1; i < n; i++) {

        if(arr[i] < min) {

            min = arr[i];

        }

    }

 

    // Display result

    printf("\nMinimum element in the array = %d\n", min);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 6
Enter 6 elements:
45 12 78 5 33 19


OUTPUT :
Minimum element in the array = 5

Explanation

  1. The user inputs the total number of elements (n).
  2. A for loop reads each array element.
  3. The program assumes the first element as the minimum (min = arr[0]).
  4. The next for loop traverses all elements and updates min whenever a smaller element is found.
  5. Finally, it prints the minimum element in the array.

 

C Program: Minimum in array

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0, min;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    while(i < n) {

        scanf("%d", &arr[i]);

        i++;

    }

 

    // Initialize minimum with first element

    min = arr[0];

 

    // Reset index for comparison

    i = 1;

 

    // Find minimum element using while loop

    while(i < n) {

        if(arr[i] < min) {

            min = arr[i];

        }

        i++;

    }

 

    // Display result

    printf("\nMinimum element in the array = %d\n", min);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
12 45 8 67 3

OUTPUT :
Minimum element in the array = 3

Explanation

  1. The program first asks the user to input the number of elements (n).
  2. Using a while loop, it reads all n elements into the array.
  3. It initializes min with the first array element (arr[0]).
  4. Another while loop is used to compare each element with min.
    • If a smaller element is found, min is updated.
  5. After the loop ends, the smallest element is printed.

 

C Program: Minimum in array

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0, min;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements using do...while loop

    printf("Enter %d elements:\n", n);

    do {

        scanf("%d", &arr[i]);

        i++;

    } while(i < n);

 

    // Assume first element as minimum

    min = arr[0];

 

    // Find minimum element using do...while loop

    i = 1;

    do {

        if(arr[i] < min) {

            min = arr[i];

        }

        i++;

    } while(i < n);

 

    // Display result

    printf("\nMinimum element in the array = %d\n", min);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
34 12 56 7 23

OUTPUT :
Minimum element in the array = 7

Explanation

  1. The user enters how many elements (n) the array should have.
  2. Using a dodo..while loop, all elements are read into the array.
  3. The first element is assumed as the minimum (min = arr[0]).
  4. Another do..while loop traverses the array from the second element onward.
    • If a smaller value is found, it updates min.
  5. Finally, it prints the minimum element.