to print the largest of 8 numbers in a given array pls tell fast I need program for this
Answers
Answered by
2
Answer:
This is the required program for the question in Java.
import java.util.*;
public class Max {
public static void main(String[] args) {
int a[]=new int[8],i=0,max=Integer.MIN_VALUE;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 8 numbers..");
for(;i<8;i++) {
System.out.print("Enter: ");
a[i]=sc.nextInt();
if (a[i]>max)
max=a[i];
}
System.out.println("Given List: "+Arrays.toString(a));
System.out.println("Largest Element: "+max);
sc.close();
}
}
Logic:
- We will assume that the smallest number in integer range be the largest number and store the number in max variable. To get the smallest integer, Integer.MIN_VALUE is used.
- Then, we will ask the user to enter the numbers in the list. If the number is greater than max value, we will assign the number in max value. In this way, we can get the largest number in the list.
See the attachment for output ☑.
Attachments:
Similar questions
Science,
1 month ago
History,
1 month ago
Social Sciences,
3 months ago
Math,
3 months ago
Math,
9 months ago