A Java program to find the largest and smallest in a array;
public class array {
public static void main(String[] args) {
int numbers[] = new int[]{12,34,1034,34,56,67,89,01,345,235,56};
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
To compile follow following steps:-
public class array {
public static void main(String[] args) {
int numbers[] = new int[]{12,34,1034,34,56,67,89,01,345,235,56};
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
To compile follow following steps:-
- Save the code with file name array
- Open terminal or cmd and move to the file location.
- Type javac array.java
- Type java array
No comments:
Post a Comment