Find elements

C program to find smallest and largest number in an array

C program to find largest and smallest number in an array

In this tutorial, we will discuss the concept of a C program to find the largest and smallest number in an array

In this topic, we learn how to find the smallest and largest element of an array(Collection of elements)

This program gets “n” number of elements and Enter the elements of the array as input from the user. then, this program finds and displays the smallest and largest elements from the array using for loops.

In this programs, we can see step by step procedure for completion of the program.

  • Declare an array of fixed capacity
  • Essential variable declaration
  • Taking the size of the array as input from the user
  • Taking the elements of the array as input from the user
  • loop for find the smallest and largest value
  • Display result on the screen

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int arr[30];//array declaration
    int length,i;
    int min,max; //essential variable declaration
    printf("Enter the length of the array: ");
    scanf("%d", &length);//get input from user for length
    for(i=0; i<length; i++){
    printf("Enter element %d: ",i+1);//enter elements for array
    scanf("%d", &arr[i]);
    }
    max=arr[0];//assume first element as largest number
      min=arr[0];//assume first element as smallest number

    for(i=0; i<length; i++){//iterate for loop from first index of array
    if(max<arr[i]){
        max=arr[i];
    }
    if(min>arr[i]){
        min=arr[i];
    }
    }
    printf("\nLargest elements is: %d",max);
    printf("\nSmallest elements is: %d",min);
getch();//display result on the screen
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter the length of the array: 5
Enter elements 1: 45
Enter elements 2: 12
Enter elements 3: 98
Enter elements 4: 65
Enter elements 5: 34

Largest elements is: 98
Smallest elements is: 12

 

Similar post

 largest number of three number in C

 largest number of three number in C++

 largest number of three number in Python

 largest number of three number in C using function

 largest number of three number in C++ using function

 largest number of three number in Python using function

 largest number of three number in Java using method

 

Suggested for you

Operator in C language with example

For loop in C language 

If statements in C language

Data type in C language

Single dimension array in C language

Two dimension array in C language

Three dimension array in C language

C program to find largest elements of an Array
C program to find smallest number in an array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago