C Program to find maximum and minimum number in an array

Category: C ProgramTags: cprogram function

C Program to find the maximum and minimum number in an array

C Program to find maximum and minimum number in an array

#include <stdio.h>

int minOfArray(int[], int);
int maxOfArray(int[], int);

void main()
{
    int size, arr[50], i;

    printf("Enter size of array: ");
    scanf("%d", &size);
    printf("Enter elements of array-\n");
    for (i = 0; i < size; i++)
        scanf("%d", &arr[i]);

    printf("\nMinimum number: %d\n", minOfArray(arr, size));
    printf("Maximum number: %d", maxOfArray(arr, size));
}

int minOfArray(int arr[], int size)
{
    int i, min = arr[0];
    for (i = 0; i < size; i++)
    {
        if (arr[i] < min)
            min = arr[i];
    }
    return min;
}

int maxOfArray(int arr[], int size)
{
    int i, max = arr[0];
    for (i = 0; i < size; i++)
    {
        if (arr[i] > max)
            max = arr[i];
    }
    return max;
}

Output

Output

Enter size of array: 5
Enter elements of array-
2
3
1
6
4
Minimum number: 1
Maximum number: 6