Searching...
Friday, 9 December 2016

Program to find out Largest and Smallest Value in an Array

07:57

Java program

package com.vinod.test;

/**
 *@authorvinodkariyathungalkumaran
 *
 */
public class FindLargestAndSmallestNumber {

    public static void main(String[] args) {

        int numbers[] = new int[] { 35, 21, 44, 55, 22, 1, 7 };
        int smallest = numbers[0];
        int largest = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > largest) {
                largest = numbers[i];
            }
            if (numbers[i] < smallest) {
                smallest = numbers[i];
            }
        }
        System.out.println("Largest value " + largest);
        System.out.println("Smallest value " + smallest);
    }
}

Output

Largest value 55
Smallest value 1

 

0 comments: