Home » C program to find minimum and maximum number in an array

C program to find minimum and maximum number in an array

Below is the code to find minimum and maximum number in an array

#include <stdio.h>
int main(){
    int n,i;
    printf("Enter how many elements do you want to enter: ");
    scanf("%d",&n);
    int arr[n];
    printf("Enter elements in an array\n");
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    int m1,m2;
    m1=m2=arr[0];
    for(i=0;i<n;i++){
        if(arr[i]>m1){
            m1=arr[i];
        }
        if(arr[i]<m2){
            m2=arr[i];
        }
    }
    printf("Maximum number is %d\n",m1);
    printf("Minimum number is %d",m2);
}

Contents

Output:

Enter how many elements do you want to enter: 4
Enter elements in an array
3
7
8
2
Maximum number is 8
Minimum number is 2