Find smallest number in the array.
This is simple example for finding smallest number in the array . In this example
we taken a array and sort it then iterate for the first position to find smallest number .
import java.util.Arrays;
public class TestClass {
public static void myArry(){
int []a= {4,1,98 ,43 ,22,2} ;
System.out.println(" Given Array");
for (int i : a) {
System.out.println(i);
}
Arrays.sort(a);
System.out.println("Sorted Array");
for (int i : a) {
System.out.println(i);
}
for( int i=0 ; i<a.length ; i++) {
System.out.println(a[0]);
break;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestClass.myArry();
}
}
Output of the code will be :
Given Array
4
1
98
43
22
2
Sorted Array
1
2
4
22
43
98
smallest number=1
No comments:
Add your comment