answersLogoWhite

0


Best Answer

{

// set up our input buffer

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String currentLine;

// store our ints in a list

int smallest = 0;

int largest = 0;

// counter

int numIntsRead = 0;

// force smallest and largest to start as the first proper int entered

while (numIntsRead < 1) {

currentLine = in.readLine();

try {

// convert from string to int

int currentNumber = Integer.parseInt(currentLine);

smallest = currentNumber;

largest = currentNumber;

++numIntsRead;

} catch (final NumberFormatException ex) {

// we go here if the user didn't type in an integer

}

}

// loop until we read all 5 ints

while (numIntsRead < 5) {

currentLine = in.readLine();

try {

// convert from string to int

int currentNumber = Integer.parseInt(currentLine);

if (currentNumber < smallest) {

smallest = currentNumber;

}

if (currentNumber > largest) {

largest = currentNumber;

}

++numIntsRead;

} catch (final NumberFormatException ex) {

// we go here if the user didn't type in an integer

}

}

// display our findings

System.out.println("Smallest:\t" + smallest);

System.out.println("Largest:\t" + largest);

}

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Read five integers and prints the largest and smallest integer using java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a program that randomly fills a 10 component array then prints the largest and smallest values in the array?

final double[] ns = new double[10]; final Random rnd = new Random(System.currentTimeMillis()); // Fill... for (int i = 0; i &lt; ns.length; ++i) { ns[i] = rnd.nextDouble(); } // Get largest/smallest... double largest = Double.MIN_VALUE; double smallest = Double.MAX_VALUE; for (double n : ns) { if (n &gt; largest) { largest = n; } if (n &lt; smallest) { smallest = n; } } // largest and smallest are now the proper values.


Write a C program that will ask the user for 4 integer values and then produce the following output 1 The sum of the four numbers 2 The sum of the first two numbers minus the sum of the las?

#include&lt;stdio.h&gt; main() { int a,b,c,d; // The four integers to be asked printf("Give the first integer: "); //asks for the first integer scanf("%d",&amp;a); // puts the user input in the address of the integer "a" printf("Give the second integer: "); //same explanations scanf("%d",&amp;b); printf("Give the third integer: "); scanf("%d",&amp;c); printf("Give the fourth integer: "); scanf("%d",&amp;d); printf("1. The sum of the four integers is: %d",a+b+c+d); //prints the sum of the four integers given by the user, notice the "a+b+c+d" at the end) printf("2. The sum of the first two numbers minus the sum of the last: %d",a+b-c-d); //prints the second condition by putting the correct operations return 0; //ends the program } I never tested this program though, but i think it would work.


Write a program that input a positive integer and prints a triangle using for loop?

write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?


Write a function that reads an integer and determines and prints how many digits in the integer are 7s?

== == // Returns number of 7s in num. int numSevens(int num) { int _num = num; int numSevens = 0; while( _num &gt; 0 ) { if( (_num % 10) == 7 ) { ++numSevens; } _num /= 10; } return numSevens; }


Write a c plus plus program that inputs 5 integers from the user and separates the integer into individual digits and prints the digits separated from one another by three spaces each.?

// create an BufferedReader from the standard input stream BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String currentLine = ""; int total = 0; int[] ints = new int[5]; // read integers while (total &lt; 5) { // get input System.out.print("Input an integer: "); currentLine = in.readLine(); // parse as integer try { int input = Integer.parseInt(currentLine); ints[total] = input; ++total; } catch (final NumberFormatException ex) { System.out.println("That was not an integer."); } } // print each number for (int i = 0; i &lt; ints.length; ++i) { // get individual digits if (ints[i] == 0) { System.out.println(0); } else { while (ints[i] &gt; 0) { System.out.println(ints[i] % 10); ints[i] /= 10; } } } Note that this prints out the digits in reverse order (2048 will print 8 first and 2 last).

Related questions

Write a program that randomly fills a 10 component array then prints the largest and smallest values in the array?

final double[] ns = new double[10]; final Random rnd = new Random(System.currentTimeMillis()); // Fill... for (int i = 0; i &lt; ns.length; ++i) { ns[i] = rnd.nextDouble(); } // Get largest/smallest... double largest = Double.MIN_VALUE; double smallest = Double.MAX_VALUE; for (double n : ns) { if (n &gt; largest) { largest = n; } if (n &lt; smallest) { smallest = n; } } // largest and smallest are now the proper values.


Write a C program that will ask the user for 4 integer values and then produce the following output 1 The sum of the four numbers 2 The sum of the first two numbers minus the sum of the las?

#include&lt;stdio.h&gt; main() { int a,b,c,d; // The four integers to be asked printf("Give the first integer: "); //asks for the first integer scanf("%d",&amp;a); // puts the user input in the address of the integer "a" printf("Give the second integer: "); //same explanations scanf("%d",&amp;b); printf("Give the third integer: "); scanf("%d",&amp;c); printf("Give the fourth integer: "); scanf("%d",&amp;d); printf("1. The sum of the four integers is: %d",a+b+c+d); //prints the sum of the four integers given by the user, notice the "a+b+c+d" at the end) printf("2. The sum of the first two numbers minus the sum of the last: %d",a+b-c-d); //prints the second condition by putting the correct operations return 0; //ends the program } I never tested this program though, but i think it would work.


Write a C program that prompts the user for three decimal numbers and prints them trimmed as integer values. For example, if the user informs the values 13.2, 12.5, 102.231, the program prints out 13, 12, 102?

Here's a simple C program that prompts the user for three decimal numbers, converts them to integers, and prints the trimmed integer values: c Copy code #include int main() { double num1, num2, num3; // Prompt the user for three decimal numbers printf(&quot;Enter three decimal numbers: &quot;); scanf(&quot;%lf %lf %lf&quot;, &amp;num1, &amp;num2, &amp;num3); // Convert and print the trimmed integer values int intNum1 = (int)num1; int intNum2 = (int)num2; int intNum3 = (int)num3; printf(&quot;Trimmed integer values: %d, %d, %d\n&quot;, intNum1, intNum2, intNum3); return 0; } In this program: We declare three variables num1, num2, and num3 to store the decimal numbers entered by the user. We use printf to prompt the user to enter three decimal numbers. We use scanf to read and store the user's input in the variables num1, num2, and num3. We then convert these decimal numbers to integers by casting them using (int) and store them in intNum1, intNum2, and intNum3. Finally, we use printf again to print the trimmed integer values. Compile and run the program, and it will prompt you for three decimal numbers and print their trimmed integer values as requested.


Write a program that input a positive integer and prints a triangle using for loop?

write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?


How much maximum value Us dollar note?

The largest denomination of currency the US prints today is the $100 bill.


Who is Thomas prints?

thomas prints invented prints


What is definition for dry wet prints?

wet prints are those in which fabri is either dyed or printed dry prints are the fancy prints like flock prints, burn out prints.


Difference between percent p and percent x in c language?

%p prints a pointer, %x prints an integer. They may be similar, but not the same. Eg.printf ("ptr=%p int=%d\n", main, (int)main);DOS: ptr=0F01:0010 int=10Windows: ptr=:0F010010 int=F010010


How can Implement JAVA code which takes 2 dimensional integer array as input and prints out heaviest island?

public static void main (String args[]) throws IOException { int abc = System.in.read(); }


Write a function that reads an integer and determines and prints how many digits in the integer are 7s?

== == // Returns number of 7s in num. int numSevens(int num) { int _num = num; int numSevens = 0; while( _num &gt; 0 ) { if( (_num % 10) == 7 ) { ++numSevens; } _num /= 10; } return numSevens; }


Write a c plus plus program that inputs 5 integers from the user and separates the integer into individual digits and prints the digits separated from one another by three spaces each.?

// create an BufferedReader from the standard input stream BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String currentLine = ""; int total = 0; int[] ints = new int[5]; // read integers while (total &lt; 5) { // get input System.out.print("Input an integer: "); currentLine = in.readLine(); // parse as integer try { int input = Integer.parseInt(currentLine); ints[total] = input; ++total; } catch (final NumberFormatException ex) { System.out.println("That was not an integer."); } } // print each number for (int i = 0; i &lt; ints.length; ++i) { // get individual digits if (ints[i] == 0) { System.out.println(0); } else { while (ints[i] &gt; 0) { System.out.println(ints[i] % 10); ints[i] /= 10; } } } Note that this prints out the digits in reverse order (2048 will print 8 first and 2 last).


How do you make glass prints?

when you put your finger prints on the glassthen their is finger prints