GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Java Program to Find Largest Number in an Array
Java Program to Find Largest Number in an ArrayThis article is created to cover multiple programs in Java that find the largest number in an array, entered by user at run-time of the program. Java Find Largest Number in Array using for LoopThe question is, write a Java program to find and print the largest number in an array of 10 numbers. The program given below is its answer: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { int i, large; int[] arr = new int[10]; Scanner scan = new Scanner(System.in); System.out.print("Enter 10 Numbers: "); for(i=0; i<10; i++) arr[i] = scan.nextInt(); large = arr[0]; for(i=1; i<10; i++) { if(large<arr[i]) large = arr[i]; } System.out.println("\nLargest Number = " +large); } } The snapshot given below shows the sample run of above program, with user input 50, 51, 52, 53, 59, 58, 54, 55, 56, 57 as ten numbers for the array to find and print the largest one, among all these ten numbers: The above program only works with 10 numbers. Therefore let's modify the program and create another, that find and prints largest number in an array of n numbers: import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { int n, i, large; Scanner scan = new Scanner(System.in); System.out.print("Enter the Value of n: "); n = scan.nextInt(); int[] arr = new int[n]; System.out.print("Enter " +n+ " Numbers: "); for(i=0; i<n; i++) arr[i] = scan.nextInt(); large = arr[0]; for(i=1; i<n; i++) { if(large<arr[i]) large = arr[i]; } System.out.println("\nLargest Number = " +large); } } The sample run with user input 6 as size or value of n and 1, 2, 3, 4, 5, 6 as six elements or numbers is shown in the snapshot given below: Java Find Largest Number in Array using while LoopHere is the last program of this article, create using while loop, instead of for. import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter the Value of n: "); int n = scan.nextInt(); int[] arr = new int[n]; System.out.print("Enter " +n+ " Numbers: "); int i=0; while(i<n) { arr[i] = scan.nextInt(); i++; } int large = arr[0]; i = 1; while(i<n) { if(large<arr[i]) large = arr[i]; i++; } System.out.println("\nLargest Number = " +large); } } This program produces same output as of previous program. Same Program in Other Languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |