C Program to Find Largest and Smallest Number among N Numbers

C program to find the largest and smallest number among N numbers entered by the user is shown below

Source code to find largest and smallest number

#include<stdio.h>
int main()
{
    int i, n, lar,sm, elem;
    printf ("Enter total number of elements n");
    scanf ("%d", &elem);
    printf ("Enter first number n");
    scanf ("%d", &n);
    lar = n;
    sm=n;
    for (i=1; i<= elem -1 ; i++)
    {
        printf ("n Enter another number n");
        scanf ("%d",&n);
        if (n>lar)
        lar=n;
        if (n<sm)
        sm=n;
    }
    printf ("n The largest number is %d", lar);
    printf ("n The smallest number is %d", sm);
    return 0;
}

Here, the program asks the user to input total number of elements among which the largest and the smallest is to be found. It then asks for the first number from the user before the loop, which is assigned to both variable lar and variable sm. Here, we suppose that lar is the largest number and sm is the smallest number for now.
Now inside the loop, the program asks the user to input a number (n -1) times (n-1 times as first number is already asked before the loop). Each time the user inputs a number, the condition n>lar is checked; if the entered number is greater than lar, lar=n which assigns the latest entered number to lar implying n as the new greatest.. Similarly, the condition n<sm is also checked; if the entered number is smaller than sm then sm=n implying n as the new smallest. When the program exits the loop, greatest number stored in lar and smallest number stored in sm is displayed.

Output

Enter total number of elements
10
Enter first number
3

Enter another number
8

Enter another number
12

Enter another number
42

Enter another number
89

Enter another number
45

Enter another number
236

Enter another number
890

Enter another number
411

Enter another number
328

The largest number is 890
The smallest number is 3