Find the smallest element in the array

Problem statement:- Program to Find the minimum element in the array.

Sample Input/Output:-

Sample Input First:  5                        Sample Output First: 6                            

                                    6
                                   20 
                                    9
                                   35
                                   47

Sample Input Second:  4                     Sample Output Second: 2                            

                                        25
                                        4 
                                        8
                                        2

Data requirement:-

   Input Data:- arr,size

  Output Data:-min

  Additional Data:- i, j

Program in C

Here is the source code of the C Program to Find the smallest element in the array.

Code:

#include<stdio.h>
#include<limits.h>
int main()
{

    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size],i=0,min=INT_MAX,j=0;
    printf("Enter the Element of the array:\n");
     while(j<size)
    {
        scanf("%d",&arr[j]);
        j++;

    }
    while(i<size)
    {
        if(arr[i]<=min)
        min=arr[i];
        i++;
    }
    printf("The smallest element of array: %d",min);
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
20
6
9
35
47
The smallest element of array: 6

Program in C++

Here is the source code of the C+ Program to find the minimum element in the array.

Code:

#include<iostream>
#include<climits>
using namespace std;
int main()
{

    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size],i=0,min=INT_MAX,j=0;
    cout<<"Enter the Element of the array:\n";
     while(j<size)
    {
        cin>>arr[j];
        j++;

    }
    while(i<size)
    {
        if(arr[i]<=min)
        min=arr[i];
        i++;

    }
    cout<<"The smallest element of array: "<<min;
}

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
25
4
8
2
The smallest element of array: 2

Program in Java

Here is the source code of the Java Program to Find the smallest element in the array.

Code:

import java.util.Scanner;
public class p7 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int size;
    size=sc.nextInt();
    int arr[ ]=new int[size];
int i=0,min=Integer.MAX_VALUE,j=0;
System.out.println("Enter the Element of the array:");
     while(j<size)
    {
        arr[j]=sc.nextInt();
        j++;
    }
    while(i<size)
    {
        if(arr[i]<=min)
        min=arr[i];
        i++;

    }
    System.out.println("The smallest element of array: "+min);
    sc.close();
}
}

Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
20
3
9
2
1
The smallest element of array: 1

Program in Python

Here is the source code of the Python Program to find the minimum element in the array.

Code:

import sys
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
min=sys.maxsize
for j in range(0,size):
    if (arr[j] <= min):
        min = arr[j]

print("The smallest element of array: ",min)

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
99
2
4
999
100
The smallest element of array:  2

Post a Comment

0 Comments