Knowledge Walls
Java Master
Hyderabad, Andhra Pradesh, India
Find Big and Small Number from a Array in Java
8798 Views
Output 
Enter the num
12
23
34
10

12==12
23==12
34==23
10==34
Lareg Num:34=Small Num=10
EasiestExample
import java.util.Arrays;
import java.util.Collections;

class EasiestExample {
    public static void main(String args[]) {
     Integer numbers[] = {10,20,40};
     System.out.println(Collections.max(Arrays.asList(numbers)));
     System.out.println(Collections.min(Arrays.asList(numbers)));
    }
}
Output 
40
10
Hints 
First example is used to find large and small number using algorithm. and the second one is already created algorith with java standard edition library and Arrays.asList used to convert array of string to List of string. Collections.max returning big number in the list. and Collections.min returning small number from the list.
MyObjectExample
import java.util.Scanner;

class MyObjectExample {
    public static void main(String args[]) {

        System.out.println("Enter the num");
        Scanner a = new Scanner(System.in);
        int x = a.nextInt();
        int y = a.nextInt();
        int z = a.nextInt();
        int x1 = a.nextInt();

        int[] arr = { x, y, z, x1 };

        int large = arr[0];
        int small = arr[0];

        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] + "==" + large);
            if (arr[i] > large) {
                large = arr[i];
            }
            if (arr[i] < small) {
                small = arr[i];
            }

        }

        System.out.println("Lareg Num:" + large + "=Small Num=" + small);

    }
}
Previous Topics
Previous lessons of current book.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details