The Big One
by CodeChum Admin
Remember that time when we've tried identifying the largest digit among the given integer? Well, let's take it to the next level, and figure out the same thing on arrays/lists!
Let's do this one more time!
Instructions:
Create a variable that accepts a positive integer.
Create an empty list. Then, using loops, add random integer values into the list one by one. The number of values to be stored is dependent on the inputted positive integer given in the first instruction.
Print out the largest integer among all the elements on the list, by any means necessary.
import java.util.*;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter array size: ");
int size = keyBoard.nextInt();
int numbers[] = new int[size];
for (int i = 0; i < size; i++) {
System.out.print("Enter the number: ");
numbers[i] = keyBoard.nextInt();
}
int max = numbers[0];
for (int i = 1; i < size; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
System.out.println("The largest integer: " + max);
keyBoard.close();
}
}
Comments
Leave a comment