WAP in C++ & Python to find the largest & smallest element in Array

By | September 7, 2019

Here in this program, we will code to find the largest and smallest element from a 1D Array.

C++ Program to find the largest and smallest elements from an Array:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int arr[100],num, elements, min, max;
cout<<"How many elements you want to enter?: ";
cin>>num;
cout<<"Enter the elements in the array\n";
for(int i=0; i<num;i++)
   cin>>arr[i];
max=arr[0];
min=arr[0];
for(i=0; i<num; i++)
         {
          if(arr[i]>max)
                max= arr[i];
          if(arr[i]<min)
                min= arr[i];
         }
cout<<"The largest Element is: "<<max<<endl;
cout<<"The smallest Element is: "<<min<<endl;
getch();
}

Output:

How many elements you want to enter?: 5
Enter the elements in the array

3
1
4
5
23

The largest Element is: 23
The smallest Element is: 1

Python Program to find the largest and smallest elements from an Array:

arr=[]
num = int(input("How many elements you want to enter?: "))
print("Enter the elements in the array")
for i in range(num):
    element = int(input())
    arr.append(element)
print("The largest Element is:",max(arr))
print("The smallest Element is:",min(arr))

Output:

How many elements you want to enter?: 7
Enter the elements in the array

20
234
2345
23
55
345
656

The largest Element is: 2345
The smallest Element is: 20

Leave a Reply

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