Java Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 19: Write a Java program to calculate the smallest of the elements present in an array.

import java.util.Scanner;

public class SmallestElement
	public static void main(String[] args) 
		int n;
		Scanner s = new Scanner(;
		System.out.println("Enter the number of elements to be included in the array");
		//Input Elements in array
		int a[] = new int[n];
		System.out.println("Enter elements of array");
		System.out.print("Enter 1 element: ");
		int smallest = a[0];
		for(int i=1; i < n; i++)
			System.out.print("Enter "+(i+1)+" element: ");

			if(a[i] < smallest)
				smallest = a[i];
		System.out.println("Corresponding Array: ");
		for(int i=0; i < n; i++)
			System.out.print(a[i]+" ");

		System.out.println("Smallest element in the array: "+smallest);

If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.