1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
olga55 [171]
3 years ago
6

java Two smallest numbers Write a program that reads a list of integers, and outputs the two smallest integers in the list, in a

scending 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 and 3 To achieve the above, first read the integers into an array. Hint: Make sure to initialize the second smallest and smallest integers properly.
Computers and Technology
1 answer:
SashulF [63]3 years ago
7 0

Answer:

The code to this question can be defined as follows:

import java.util.*;//import package for user input

public class Main //defining a class

{

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

{

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

   Scanner incx = new Scanner(System.in);//creating Scanner class object

   m1 = incx.nextInt();//input value

   m2 = incx.nextInt();//input value  

   if (m1 > m2)//use if block to check m1 greater than m2  

   {//swapping value

       t = m1;//holding m1 value in t

       m1 = m2;//defining m1 that take m2

       m2 = t;//holding m2 value  

   }

   for (i = 2; i < 6; i++)//defining for loop  

   {

       num = incx.nextInt();//input value  

       if (num < m1) //defining if block to smallest value

       {

           m2 = m1;//holding m2 value in m1

           m1 = num;//holding num value in m1

       }  

       else if (num < m2)//defining if block to smallest value  

       {

           m2 = num;//holding num value in m2

       }

   }

       System.out.println(m1 + " " + m2);//print two smallest values

   }

}

Output:

5

10

5

3

21

2

2 3

Explanation:

In the above code, five integer variable "m1, m2, num, t, and i" is declared, in which it creates the scanner class object for inputs the value and use m1 and m2 use to the input value.

In the next step, if a block is used to swap the value and pass into the for loop that use if block to find the two smallest values and hold its value into the m1 and m2 and print its value.

You might be interested in
Write a simple hello world program in python 3
IRINA_888 [86]

Answer:

print("hello world")

Explanation:

a hello world program is simply a program that prints out hello world.

for this you would need to remember to have the same number of brackets on each side and to write print. Also remember when printing to include speech marks.

6 0
3 years ago
Read 2 more answers
Which statement is most likely to be true about a computer network?
emmasim [6.3K]

<em>Which statement is most likely to be true about a computer network?</em>

<em>A network can have several client computers and only one server.</em>

4 0
3 years ago
Read 2 more answers
1.
Vlad1618 [11]
A. Since it tells about how people think she’s lesser because she’s a women.
7 0
2 years ago
To draw a clustered cylinder chart, first select the data to be charted and then click the column button (insert tab | charts gr
d1i1m1o1n [39]
It is true that to <span>draw a clustered cylinder chart, first select the data to be charted and then click the column button (insert tab | charts group).</span>
5 0
3 years ago
Office ____ is the current version of Microsoft Office for Mac operating systems.
gregori [183]

The current version of office is "Office 19" most people use "Office 365" though.

A "Pdf" or "Portable Document Format."

4 0
3 years ago
Other questions:
  • "is it important to have a firewall on a home network
    10·1 answer
  • Why are many otherwise safety-conscious people victims of Internet crime?
    13·1 answer
  • 3. Which of the following phase types will make a sound louder? In-phase, inverted phase, or partial phase angle waves?
    7·2 answers
  • Write a program that requests a state abbreviation as input and displays the justices appointed from that state. The justices sh
    6·1 answer
  • What is a IT form? any help would be great thnx
    14·2 answers
  • The mac group does not replace the primary functions of eocs or other dispatch organizations. True or False
    8·1 answer
  • Match the item to the type.
    11·1 answer
  • The purpose of a database is to help people stop using spreadsheets.
    10·1 answer
  • It is important to know the terms of use of any website because why
    12·2 answers
  • What is meant by Information Retrieval?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!