Minimum element in an array in Java
Q. Write a Java program to find minimum element in an array
import java.util.Scanner;
public class MinInArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user for the number of elements
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
// Read array elements
System.out.println("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
// Initialize min with the first element
int min = arr[0];
// Loop through the array to find the minimum
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
// Display the minimum element
System.out.println("Minimum element in the array is: " + min);
scanner.close();
}
}
Q. Write an algorithm to find minimum element in an array
1. Start
2. Read n (number of elements)
3. Read n integers into an array arr[]
4. Initialize min = arr[0]
5. Loop from i = 1 to n - 1:
a. If arr[i] < min, then set min = arr[i]
6. Print min
7. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com