codescracker


java

Java Program to Find Smallest Element in Array



« Previous Program Next Program »


Find Smallest Element in Array

To find the smallest element in an array in Java programming, you have to ask to the user to enter the size and elements of the array, now start finding for the smallest element in the array to display the smallest element of the array on the screen as shown in the following program.

Java Programming Code to Find Smallest Element in Array

Following Java Program ask to the user to enter array size and array elements to find the smallest element present in the array, then display the result (smallest element) on the screen:

/* Java Program Example - Find Smallest Element in Array */
		 
import java.util.Scanner;

public class JavaProgram
{
   public static void main(String args[])
   {
       int small, size, i;
       int arr[] = new int[50];
       Scanner scan = new Scanner(System.in);
	   
       System.out.print("Enter Array Size : ");
       size = scan.nextInt();
	   
       System.out.print("Enter Array Elements : ");
       for(i=0; i<size; i++)
       {
           arr[i] = scan.nextInt();
       }
	   
       System.out.print("Searching for the Smallest Element....\n\n");
	   
       small = arr[0];
	   
       for(i=0; i<size; i++)
       {
           if(small > arr[i])
           {
               small = arr[i];
           }
           
       }
	   
       System.out.print("Smallest Element = " + small); 
   }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program find smallest element in array

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test