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

1,681 views
1,349 views

Published on

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

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,681
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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

  1. 1. //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(); }
  2. 2. 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] + " "); } }
  3. 3. 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(); } }
  4. 4. OUTPUT:-

×