Java code to find smallest number in an array
- Home
- Find elements
- Java code to find smallest number in an array
- On
- By
- 0 Comment
- Categories: Find elements
Java code to find smallest number in an array
Java code to find smallest number in an array
In this tutorial, we will discuss the concept of Java code to find the smallest number of an Array
In this topic, we learn how to find the smallest element of an array(Collection of elements)
This program gets “n” number of elements and Enter the elements of the array as input from the user. then, this program finds and displays the smallest elements from the array using for loops.
In this programs, we can see step by step procedure for completion of the program.
- array declaration
- Essential variable declaration
- Get input from the user for array length
- Get input from the user for array elements
- loop for finding the smallest value
- Display result on the screen
Find the smallest value – integer Array
import java.util.Scanner; class Small_num1{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); System.out.print("Enter the number of elements in an array: "); int min; int n=scan.nextInt();//get input from user for array length int arr[]=new int[n]; //declaring an array of n elements //for loop takes input from user for(int i=0; i<n; i++){ System.out.print("Enter the element "+(i+1)+": "); arr[i]=scan.nextInt();//takes input from user for array } min=arr[0]; for(int i=0; i<n; i++){ if(min>arr[i]){//loop for find smallest elements in array min=arr[i]; } } System.out.print("\nThe smallest elements is: "+min); }//display result on the result }
When the above code is compiled and executed, it produces the following results
Enter the number of elements in an array: 5 Enter the element 1: 45 Enter the element 2: 32 Enter the element 3: 56 Enter the element 4: 23 Enter the element 5: 78 The smallest elements is:23
Find the smallest value – float Array
Program 1
import java.util.Scanner; class Small_num{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); System.out.print("Enter the number of elements in an array: "); float min; int n=scan.nextInt();//get input from user for array length float arr[]=new float[n]; //declaring an array of n elements //for loop takes input from user for(int i=0; i<n; i++){ System.out.print("Enter the element "+(i+1)+": "); arr[i]=scan.nextFloat();//takes input from user for array } min=arr[0]; for(int i=0; i<n; i++){ if(min>arr[i]){ min=arr[i]; } } System.out.print("\nThe smallest value is: "+min); }//display result on the result }
When the above code is compiled and executed, it produces the following results
Enter the number of elements in an array: 5 Enter the element 1: 4.6 Enter the element 2: 3.1 Enter the element 3: 6.7 Enter the element 4: 5.3 Enter the element 5: 1.3 The smallest value is: 1.3
Similar post
largest number of three number in C
largest number of three number in C++
largest number of three number in Python
largest number of three number in C using function
largest number of three number in C++ using function
largest number of three number in Python using function
largest number of three number in Java using method
Suggested for you
If statements in Java language
Single dimension array in Java language
Two dimension array in Java language
Three dimension array in Java language
Operator in C language with example
Single dimension array in C language