Java program that read a sequence of integer inputs and print the smallest and largest of the inputs
Posted by Samath
Last Updated: April 03, 2022
  175

Write a program that read a sequence of integer inputs and print the smallest and largest of the inputs.

Code:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	    
		   Scanner input = new Scanner(System.in);
	       
	       double num;
	       double s_num;
	       double l_num;
	       
	        System.out.print("Enter Number (-1 to end input): ");
	       num = input.nextDouble();
	       
	        s_num = num;
	       l_num = num;
	       
	       while (num != -1) {
	            System.out.print("Enter Number (-1 to end input): ");
	            num = input.nextDouble();
	            if (num < s_num && num != -1) {
		            s_num = num;
	            }
	            else if (num > l_num && num != -1) {
		            l_num = num;
	            }
	        }
	       System.out.println("Smallest number: " + s_num);
	       System.out.println("Largest number: " + l_num);
	       input.close();
	}
}