A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Java program to find smallest element in an array
Q:

Java program to find smallest element in an array

0

Given an array of N integers and we have to find its minimum/smallest element using Java program.

Example:

    Input:
    Enter number of elements: 4
    Input elements: 45, 25, 69, 40

    Output:
    Smallest element in: 25

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find smallest element from an array in java

import java.util.Scanner;

public class ExArrayFindMinimum {
  public static void main(String[] args) {
    // Intialising the variables
    int n, min;
    Scanner Sc = new Scanner(System.in);

    // Enter the number of elements.
    System.out.print("Enter number of elements : ");
    n = Sc.nextInt();

    // creating an array.
    int a[] = new int[n];

    // enter array elements.
    System.out.println("Enter the elements in array : ");
    for (int i = 0; i < n; i++) {
      a[i] = Sc.nextInt();
    }

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (a[i] > a[j]) {
          min = a[i];
          a[i] = a[j];
          a[j] = min;
        }
      }
    }
    System.out.println("The Smallest element in the array is :" + a[0]);
  }
}

Output

Enter number of elements : 4
Enter the elements in array : 
45
25
69
40
The Smallest element in the array is :25

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now