In this core java programming tutorial we will Write a program to find Smallest And Largest Number In Array In Java.
Full Program/SourceCode / Example to find Smallest And Largest Number In Array In Java>
/**
* Write a program to find Smallest And Largest Number In Array In Java
*/
public class FindSmallestAndLargestNumberInArrayInJava {
static int ar[]={3,9, 4,9 , 1, 2, 5,8,12, 7}; //given array
public static void main(String[] args) {
int smallestNumber = ar[0];
int largestNumber = ar[0];
for(int i=1; i< ar.length; i++) {
if(ar[i] > largestNumber)
largestNumber = ar[i];
else if (ar[i] < smallestNumber)
smallestNumber = ar[i];
}
System.out.println("Smallest Number in array is : " + smallestNumber);
System.out.println("Largest Number in array is : " + largestNumber);
}
}
/*
Smallest Number in array is : 1
Largest Number in array is : 12
*/
|
So, in this core java programming tutorial we wrote a program to find Smallest And Largest Number In Array In Java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
Write a program to add two numbers in java
>7) Write a Program on How to find number is binary or not program in java - basic interview programs
>10) Write a Program to calculate Occurrence of digit in cube of number in java - interview programs
>14) Write a Program to Find only duplicate number in given list in java - important interview programs
>16) Write a Program to Find all duplicate numbers in list in java - tricky important interview programs