Smallest and largest element in an array

Write a program to find the smallest and largest element in an array.

Example:

Input:  arr[]={3,2,4,5,1,6,3}
Output: Smallest Number is 1
Largest number is 6

C Program

#include <stdio.h>
#include <limits.h>

int main()
{
    int arr[] = {3245163};

    int n = sizeof(arr) / sizeof(arr[0]);

    int large = INT_MIN;
    int small = INT_MAX;

    for (int i = 0; i < n; i++)
    {
        if (arr[i] > large)
        {
            large = arr[i];
        }
        if (arr[i] < small)
        {
            small = arr[i];
        }
    }
    printf("Smallest Number is %d\n", small);
    printf("Largest number is %d\n", large);

    return 0;
}


No comments:

Post a Comment