Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. You can assume that the list will have at least 2 integers and fewer than 20 integers. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 To achieve the above, first read the integers into an array. Hint: Make sure to initialize the second smallest and smallest integers properly.

Answers

Answer 1

Answer:

Following are the code to this question:

import java.util.*;//import package

public class Main //defining a Main class  

{

   public static void main(String[] args)//defining a main method  

   {

       Scanner oxc = new Scanner(System.in);//defining main method  

       int n,m1,m2,num,t,i;//defining integer variable

       n= oxc.nextInt();//input value from user end  

       m1 = oxc.nextInt(); //input value from user end

       m2 = oxc.nextInt(); //input value from user end

       if (m1 > m2)//defining if block that check m1 greater then m2  

       {

           //swapping the value

           t= m1;

           m1 = m2;

           m2 = t;

       }

       for (i=2;i<n;i++)//defining for loop that check the smallest value    

       {

           num = oxc.nextInt();//input value from user end

           if (num < m1) //defining if block that check num lessthan m1

           {

               m2 = m1;//store m1 value into m2  

               m1 = num;//store num value into m1

           }

           else if (num<m2)//defining else if block that check num lessthan m2

           {

               m2 = num;//store m2 value in num

           }

       }

       System.out.println(m1 + " " + m2);//print value

   }

}

Output:

5

10

5

3

21

2

2 3

Explanation:

In the above-given code, inside the main class the main method is declared, in method 6 integer variable "n,m1,m2, num,t, and i" is declared, and in the "n, m1, m2, and num" is used for input the value from the user end.

In the next step, and if block is defined that check m1 greater than m2, and swap the value.In the next step for loop s used that defines two conditions that check the two smallest value from the user input and print its value.

Related Questions

Other Questions
Which of the following shows some, but not all, characteristics of life? This table shows the descriptions of careers of three individuals.IndividualCareer DescriptionsDerekDrives a mobile running store in a van that sells racing gear to runners at running events, such as marathonsTaylorStands by the entrance and exit of a retail shoe store and collects money for the merchandise that customers are buyingRashidaOversees that the aquariums for a fish tank store are made, stored, and transported according to scheduleWhich best describes the career each individual has?Derek is a Driver/Sales Worker, Taylor is a Customer Service Representative, and Rashida is a Supply Chain Manager.Derek is an Entrepreneur, Taylor is a Cashier, and Rashida is a Survey Researcher.Derek is an Entrepreneur, Taylor is a Customer Service Representative, and Rashida is a Survey Researcher.Derek is a Driver/Sales Worker, Taylor is a Cashier, and Rashida is a Supply Chain Manager. Read the passage.What did the president mean when he said he had to face very difficult choices when creating a federal budget?deciding which country to borrow from to finance spendingdeciding what would be funded and what would be cutdeciding how to keep interest rates low on the national debtdeciding how much to increase tax rates group of elected officials that represented the needs of the common people of rome in the republic government Which fraction is equivalent to 0.4?StartFraction 4 over 100 EndFractionOne-fourthFour-tenthsStartFraction 4 over 1 EndFraction what is the right answer? how does gawain show loyalty to the king in sir gawain and the green knight?? Eight children sit together at story time. Seven children sit in a circle and one child sits in the middle to tell the story. In how many different ways can the children sit In the diagram, line A is parallel to line B. Which angles are congruent to angle 1?(20 Points)Select All that apply angle 2angle 3angle 4angleangle 6angle 7angle 8 could someone help please? this is hard !!PLEASE HELPP!! While reading difficult content, it is best to focus on a small section of material.Please select the best answer from the choices providedOTOF In which two ways would the story be different if it were told from the point of view of Mrs. Copperfield? The reader would know why Mrs. Copperfield is not very pleased to see Miss Betsey. The reader would know what Mrs. Copperfield looks like. The reader would know why Mrs. Copperfield is worried about herself and her son. The reader would know how old Mrs. Copperfield's son is. The reader would know why Miss Betsey has come to visit Mrs. Copperfield. PLZ HELP FASTEnter an equation in standard form to model the linear situation. A pool that is being drained contained 19,000 gallons of water. After 2 hours, 14,500 gallons of water remain. In the condition of the working class in England in 1844 how does the author show the impact of the new inventions on society? A rectangular poster is to contain 578 square inches of print. The margins at the top and bottom of the poster are to be 2 inches, and the margins on the left and right are to be 1 inch. What should the dimensions of the poster be so that the least amount of poster is used? I NEED HELP ASAP! For the first part you can pair the object with the number there is no specific one it goes with! What are some of the changes that Ann Cooper made?HURRRRRRRRRRRYYYYYYY how much force would be needed to move a 100 kg object at the acceleration rate of 2 m/s a) Solve x + 4x - 8 = 0 by completing the square Consider the enlarged triangle.A smaller triangle has side lengths 4, 5, 3. A larger triangle has side lengths 8, 10, 6.Which steps should be used to find the perimeter? Check all that apply.Find the scale factor: enlarged over original = 6 over 3 = 2Find the scale factor: enlarged over original = 3 over 6 = one-halfCalculate the perimeter of the original figure.Divide the perimeter of the original figure by the scale factor.Multiply the perimeter of the original figure by the scale factor.