QuestionAsk Questions?

make a program that enter 5 numbers then identify the largest and the smallest number

sample program 2 4 3 5 6 the smallest number: 2 the largest number: is 6

66

View Answers

November 8, 2012 at 1:31 PM

Here is a code that accepts five numbers and find the largest and smallest number from the array of input numbers.

import java.util.*; public class FindLargestSmallestNumber { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter 5 numbers: "); int numbers[] = new int[5]; for(int i=0;i<numbers.length;i++){ numbers[i]=input.nextInt(); } int smallest = numbers[0]; int largest = numbers[0]; for(int i=1; i< numbers.length; i++) { if(numbers[i] > largest) largest = numbers[i]; else if (numbers[i] < smallest) smallest = numbers[i]; } System.out.println("Largest Number is : " + largest); System.out.println("Smallest Number is : " + smallest); } }

Related Tutorials/Questions & Answers:

Advertisements