Write a program that reads a list of integers into a list as long as the integers are greater than zero, then outputs the smallest and largest integers in the list. Ex: If the input is: 10 5 3 21 2 -6 the output is: 2 21 You can assume that the list of integers will have at least 2 values.

Respuesta :

Answer:

I am writing a Python program.

integers = []

while True:

       number = int(input("Enter integers (Enter negative number to end) "))

       if number < 0:

          break

       integers.append(number)  

print ("The smallest integer in the list is : ", min(integers))

print("The largest integer in the list is : ", max(integers))

Explanation:

integers[] is the list which contains the numbers entered by the user.

while loop continues to execute until the user enters a negative value.

input() function reads the input entered by the user and int(input()) accepts and reads the int (integer) type inputs entered by the user. number holds each input entered by the user and checks if that integer is less than 0 means user enters a negative value. If it is true then the loop breaks and if it is false then the every value stored in the number is added to the list integers.

append() method adds the elements into the list.

When the user enters a negative value, the while loop breaks and the program control moves to last two print statements.

The first print() statement uses min() function which computes the minimum value in the list integers.

The second print() statement uses max() function which computes the maximum value in the given list integers.

The screen shot of program along with its output is attached.

Ver imagen mahamnasir

The code below is in Java.

It uses an indefinite while loop to get a number from the user and adds the number to the list(ArrayList) until the user enters a non-positive number.

Then, it identifies the lowest and the highest numbers in the list using for-each loop and if statement.

Comments are used to explain each line.

//Main.java

import java.util.*;

public class Main

{

public static void main(String[] args) {

    //declare the Scanner object to get input

    Scanner input = new Scanner(System.in);

   

    //declare the number and a list

    int number;

    List<Integer> numbers = new ArrayList<>();

   

    //create an indefinite while loop

    while(true){

        //get the number from the user

        number = input.nextInt();

       

        //stop the loop when the user enters a non-positive number

        if(number <= 0)

            break;

       

        //add the number to the list

        numbers.add(number);

    }

   

    //initialize the lowest and highest values

    int lowest = numbers.get(0);

    int highest = numbers.get(0);

   

    //determine the lowest and highest values in the list

    for(int n : numbers){

        if(n < lowest)

            lowest = n;

        if(n > highest)

            highest = n;

    }

   

    //print the lowest and highest values

    System.out.println("The highest: " + highest);

    System.out.println("The lowest: " + lowest);

}

}

You may check out a similar question at:

https://brainly.com/question/15020260

ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE