Program to find the maximum and minimum elements in an array.

Here, we have a basic program example to print the maximum and minimum element in an array using different languages. This program is created in c language, c++, Java, and Python.

Code to display min and max element of array in C language

#include <stdio.h>
void main()
{
    int arr1[100];
    int n, i, max, min;

       printf("Input the number of elements to be stored in the array :");
       scanf("%d",&n);

       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
	      printf("element - %d : ",i);
	      scanf("%d",&arr1[i]);
	    }
    max = arr1[0];
    min = arr1[0];

    for(i=1; i<n; i++)
    {
        if(arr1[i]>max)
        {
            max = arr1[i];
        }


        if(arr1[i]<min)
        {
            min = arr1[i];
        }
    }
    printf("Maximum element is : %d\n", max);
    printf("Minimum element is : %d\n\n", min);
}

Code to display min and max element of array in C++ language

#include <iostream>
using namespace std;

int main()
{
   int arr1[100];
    int n, i, max, min;

       cout<<"Input the number of elements to be stored in the array : ";
       cin>>n;

       cout<<"Input "<<n<<" elements in the array : \n";
       for(i=0;i<n;i++)
            {
	      cout<<"element - "<<i<<" : ";
	      cin>>arr1[i];
	    }
     max = arr1[0];
    min = arr1[0];

    for(i=1; i<n; i++)
    {
        if(arr1[i]>max)
        {
            max = arr1[i];
        }


        if(arr1[i]<min)
        {
            min = arr1[i];
        }
    }
    cout<<"Maximum element is : "<< max<<endl;
    cout<<"Minimum element is : "<< min;
}

Code to display min and max element of array in Python language

arr1=[]
n=int(input("Input the number of elements to be stored in the array : "))
print("Input ", n , " elements in the array :")
for i in range(0,n):
    l=int(input())
    arr1.append(l)
max = arr1[0]
min = arr1[0]

for i in range(1,n):
    if(arr1[i]>max):
        max=arr1[i]
    if(arr1[i]<min):
        min=arr1[i]
print("Maximum element is ", max)
print("Minimum element is ", min)

Code to display min and max element of array in Java language

import java.util.*;
public class arrays
{
  public static void main(String[] args)
   {
      int n, max, min;
     Scanner sc = new Scanner(System.in);
     System.out.print("Input the number of elements to be stored in the array : ");
     n=sc.nextInt();

    int[] arr1 = new int[n];
    int[] arr2 = new int[n];

    System.out.println("Input "+n+" elements in the array :\n");
    for(int i=0; i<n; i++)
      {
       arr1[i]=sc.nextInt();
       }
     max = arr1[0];
    min = arr1[0];

    for(int i=1; i<n; i++)
    {
        if(arr1[i]>max)
        {
            max = arr1[i];
        }


        if(arr1[i]<min)
        {
            min = arr1[i];
        }
    }
   System.out.println("Maximum element is : "+ max);
   System.out.println("Minimum element is : "+ min);
   }
}