Write a Program in Java to input ten integers and find the largest and the smallest (please write the program)
Answers
import java.util.Scanner;
import java.util.Arrays;
public class MaxMin {
public static void main(String[ ] args) {
System.out.println("Enter 10 integers - ");
// Accepting 10 Integers into an array.
int[ ] array = new int[10];
for (int i = 0; i < 10; i++)
array[i] = new Scanner(System.in).nextInt( );
// Sorting the array in ascending order.
Arrays.sort(array);
System.out.printf("%nMaximum - %d%nMinimum - %d", array[10 - 1], array[0]);
}
}
- Write a java program to input 10 integers and find the largest and smallest number from the given integers.
import java.util.*;
class MaxMin
{
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 10 numbers in the array: ");
int a[]=new int[10];
for(int i=0;i<10;i++)
{
System.out.print("["+i+"] = ");
a[i]=sc.nextInt();
}
int max=a[0], min=a[0];
for(int i=0;i<10;i++)
{
if(a[i]>max)
max = a[i];
if(a[i]<min)
min=a[i];
}
System.out.println("Largest number: "+max);
System.out.println("Smallest number: "+min);
}
}