Computer Science, asked by riddhimajain145, 8 months ago

Write a Program in Java to input ten integers and find the largest and the smallest (please write the program)

Answers

Answered by Oreki
2

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]);

}

}

Answered by anindyaadhikari13
6

\star\:\:\:\sf\large\underline\blue{Question:-}

  • Write a java program to input 10 integers and find the largest and smallest number from the given integers.

\star\:\:\:\sf\large\underline\blue{Source\:Code:-}

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);

}

}

Similar questions