Greatest and Smallest in Array


Explanation

An array is used to store multiple values of the same data type. In this program, the user enters 5 numbers into an array. The first element is assumed to be both the greatest and the smallest. A for loop then compares each array element with these values. If a smaller value is found, it becomes the new smallest value. If a larger value is found, it becomes the new greatest value. Finally, the program displays the greatest and smallest numbers.

How It Works?

  • Step 1: Declare an integer array r[5] and the variables s, g, and k.
  • Step 2: Use a for loop to accept 5 numbers from the user and store them in the array.
  • Step 3: Initialize both s and g with the first array element r[0].
  • Step 4: Use another for loop to compare each element with s and g.
  • Step 5: If the current element is smaller than s, update s.
  • Step 6: If the current element is greater than g, update g.
  • Step 7: Display the greatest and smallest values.

Example Program


#include <stdio.h>
#include <conio.h>

main()
{

    int r[5], s = 0, g = 0;
    int k;

    clrscr();

    for(k = 0; k <= 4; k++)
    {

        printf("Enter the Number : ");

        scanf("%d", &r[k]);

    }

    s = g = r[0];

    for(k = 0; k <= 4; k++)
    {

        printf("\n %d", r[k]);

        if(s > r[k])
        {

            s = r[k];

        }

        if(g < r[k])
        {

            g = r[k];

        }

    }

    printf("\n Greatest %d", g);

    printf("\n Smallest %d", s);

    getch();

}

Output

Enter the Number : 25
Enter the Number : 12
Enter the Number : 48
Enter the Number : 7
Enter the Number : 30

25
12
48
7
30

Greatest 48
Smallest 7

Practice Exercises

Task 1: Write a C program to accept 6 numbers into an array and find the greatest and smallest elements using a for loop.

Goal: Learn how to compare array elements to determine the maximum and minimum values in an array.

💡 Hint: Store 6 numbers in an array. Initialize both the greatest and smallest variables with the first element of the array. Use a for loop to compare each element and update the values whenever a larger or smaller element is found.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{

    int r[6], s = 0, g = 0;
    int k;

    clrscr();

    for(k = 0; k <= 5; k++)
    {

        printf("Enter the Number : ");

        scanf("%d", &r[k]);

    }

    s = g = r[0];

    for(k = 0; k <= 5; k++)
    {

        if(s > r[k])
        {

            s = r[k];

        }

        if(g < r[k])
        {

            g = r[k];

        }

    }

    printf("\nGreatest = %d", g);

    printf("\nSmallest = %d", s);

    getch();

}

Output
Enter the Number : 18
Enter the Number : 45
Enter the Number : 9
Enter the Number : 60
Enter the Number : 27
Enter the Number : 35

Greatest = 60
Smallest = 9
Explanation: The program accepts 6 numbers into an array. It initializes both the greatest and smallest variables with the first array element. A for loop compares each element with these variables. If a larger value is found, it updates the greatest value. If a smaller value is found, it updates the smallest value. Finally, the program displays the greatest and smallest numbers in the array.