4
//Ques. 5 WAP to store 10 numbers in an array and find out the largest and the smallest element of the array. import java.util.Scanner; public class main { public static void main(String[] args) { final int ARRAY_SIZE = 10; int[] numbers = new int[ARRAY_SIZE]; getValues(numbers); System.out.print("\n Here are the numbers that you entered: "); showArray(numbers); smallestValue(numbers); largestValue(numbers); } private static void getValues(int[] array) { Scanner keyboard = new Scanner(System.in); System.out.println("\n Enter a series of " + array.length + " numbers.\n "); for (int index = 0; index < array.length; index++)

WAP to store 10 numbers in an array and find out the largest and the smallest element of the array in java

Embed Size (px)

DESCRIPTION

WAP to store 10 numbers in an array and find out the largest and the smallest element of the array in Java

Citation preview

Page 1: WAP to store 10 numbers in an array and find out the largest and the smallest element of the array in java

//Ques. 5 WAP to store 10 numbers in an array and find out the largest and the smallest element of the array.

import java.util.Scanner;

public class main {

public static void main(String[] args)

{

final int ARRAY_SIZE = 10;

int[] numbers = new int[ARRAY_SIZE];

getValues(numbers);

System.out.print("\n Here are the numbers that you entered: ");

showArray(numbers);

smallestValue(numbers);

largestValue(numbers);

}

private static void getValues(int[] array)

{

Scanner keyboard = new Scanner(System.in);

System.out.println("\n Enter a series of " + array.length + " numbers.\n ");

for (int index = 0; index < array.length; index++)

{

System.out.print(" Enter Number " + (index + 1) + ": ");

array[index] = keyboard.nextInt();

}

Page 2: WAP to store 10 numbers in an array and find out the largest and the smallest element of the array in java

int[] numbers = new int[8];

int largest = numbers[0];

for (int index = 0; index < numbers.length; index++)

{

if (numbers[index] > largest)

{

largest = numbers[index];

}

}

int smallest = numbers[0];

for (int index = 1; index < numbers.length; index++)

{

if (numbers[index] > smallest)

{

smallest = numbers[index];

}

}

}

private static void showArray(int[] array)

{

for (int index = 0; index < array.length; index++)

{

System.out.print(array[index] + " ");

}

}

Page 3: WAP to store 10 numbers in an array and find out the largest and the smallest element of the array in java

private static void smallestValue(int[] numbers)

{

int smallest = numbers[0];

for (int index = 1; index < numbers.length; index++)

{

if (numbers[index] < smallest)

{

smallest = numbers[index];

}

}

System.out.print("\n The smallest is: " + smallest);

}

private static void largestValue(int[] numbers)

{

int largest = numbers[0];

for (int index = 0; index < numbers.length; index++)

{

if (numbers[index] > largest)

{

largest = numbers[index];

}

}

System.out.print("\n The largest is: " + largest);

System.out.println();

}

}

Page 4: WAP to store 10 numbers in an array and find out the largest and the smallest element of the array in java

OUTPUT:-