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
Here you can take the below-listed program If you want 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
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
Conclusion
This is all about printing the find the largest & smallest element in Array by writing code in Python and C Programming Languages. If you think there is some other topic that you think we should write an article or prepare codebase to help you then list down that in the given comment section below. People are also reading:
- WAP in C++ and python to find the root of quadratic equation
- WAP in C++ & Python & sort an Array Using Bubble Sort
- WAP in C++ & Python for the Union of Two Arrays
- WAP in C++ & Python to transpose a Matrix
- WAP in C++ and Python to find the sum of the series x+ x2/2 +x3/3 + ……. +xn/n
- WAP in C++ and Python to calculate 3X3 Matrix multiplication
- WAP in C++ and python to check whether the number is even or odd
- WAP in C++ and python to convert temperature from Fahrenheit to Celsius and vice-versa
- WAP in C++ and python to check whether the year is a leap year or not
- WAP in C++ & Python for Armstrong Number
Leave a Comment on this Post