Find elements

Cpp program to display smallest and largest number in Array

Cpp program to display smallest and largest number in Array

In this tutorial, we will discuss the  concept of Cpp program to display smallest and largest number in Array.

In this topic, we will learn how to find the smallest and largest elements of array elements in Cpp programming language

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.

  • array declaration
  • Essential variable declaration
  • Get input from the user for array length
  • Get input from the user for array elements
  • loop for largest and smallest value
  • Display result on the screen

Find smallest and largest elements from integer Array

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[30]; //array declaration
    int length,i,min,max;//essential variable declaration
    cout << "Enter the length of the array: ";
    cin>>length;
    for(i=0; i<length; i++){
        cout<<"Enter elements "<<i+1<<" :";//enter elements for array
        cin>>arr[i];//store the elements in array from the user
    }
    min=arr[0];//assume the first elements as smallest number
    max=arr[0];//assume the first elements as largest number
    for(i=0; i<length; i++){
    if(min>arr[i]){
        min=arr[i];
    }
    if(max<arr[i]){
        max=arr[i];
    }
    }
    cout<<"\nLargest elements is: "<<max;
    cout<<"\nSmallest elements is: "<<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: 6
Enter element 1: 65
Enter element 2: 35
Enter element 3: 12
Enter element 4: 90
Enter element 5: 76
Enter element 6: 43

Largest elements is: 90
Smallest element is: 12

Find smallest and largest elements from float Array

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    float arr[30],min,max; //array declaration
    int length,i;;//essential variable declaration
    cout << "Enter the length of the array: ";
    cin>>length;
    for(i=0; i<length; i++){
        cout<<"Enter elements "<<i+1<<": ";//enter elements for array
        cin>>arr[i];//store the elements in array from the user }
    }
    min=arr[0];//assume the first elements as smallest number
    min=arr[0];//assume the first elements as largest number
    for(i=0; i<length; i++){
    if(min>arr[i]){
        min=arr[i];
    }
    if(max<arr[i]){
        max=arr[i];
    }
    }
    cout<<"\nSmallest elements is: "<<min;
    cout<<"\nLargest elements is: "<<max;
    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: 6
Enter element 1: 2.6
Enter element 2: 5.6
Enter element 3: 4.3
Enter element 4: 9.6
Enter element 5: 1.2
Enter element 6: 5.3

Smallest element is: 1.2
Largest element is: 9.6

 

Suggested for you

Operator in C++ language

Single dimension Array in C++ language

For loop in C++ language

If statements in C++

Data type in C++ programming language

C program to find smallest number in an array
Cpp program to display smallest number of 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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago