C

C Program To Find Smallest Number In An Array

In this tutorial, we are going to see how to write a C program to find smallest number in an array. The following program takes N number of elements entered by the user and stores the data in an array. Then this program displays the smallest element of this array using FOR loops.
 

Algorithm:
Input: arr[] = {5, 9, 2}
Output: 2

 

 

C Program To Find Smallest Number In An Array
#include <stdio.h>

int main()
{
    int i, nbr, arr[100];
  
    printf("Enter the total number of items: ");
    scanf("%d", &nbr);
    printf("\n");
  
    // Store the numbers entered by the user
    for(i = 0; i < nbr; ++i)
    {
       printf("Enter number %d: ", i+1);
       scanf("%d", &arr[i]);
    }
    // Loop to store the max in arr[0]
    for(i = 1; i < nbr; ++i)
    {
       if(arr[0] > arr[i])
           arr[0] = arr[i];
    }
    printf("The smallest element is %d", arr[0]);
    return 0;
}

Output:

Enter the total number of items: 5

Enter number 1: 6
Enter number 2: 2
Enter number 3: 9
Enter number 4: 4
Enter number 5: 1

The smallest element is 1

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *