In: Computer Science
This assignment involves developing a program that prompts the user to enter a series of 10 integers and then determines and displays the largest and smallest values entered.
Your solution must use at least the following variables: counter: A counter to count how many integers were entered
a Number: The integer most recently input by the user,
smallest: The smallest number entered so far, largest: The largest number entered so far.
Write three separate programs for this assignment:
Name the first program HiLoWhile and use a while construct in the solution.
Name the second program HiLoFor and use a for construct in the solution.
Name the third program HiLoDoWhile and use a do-while construct in the solution.
Write the pseudocode for ONLY the HiLoWhile program.
IN JAVA
The programs are written below, and they all use the variables required above, and process the 10 integer inputs to calculate and print the smallest and largest values.
The HiLoWhile program is:
import java.util.Scanner; // Import Scanner class to get user input
public class Main{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
int counter = 0;
int latest_input = 0;
int smallest = 0;
int largest = 0;
int i = 0;
while (i < 10) {
int input = scan.nextInt();
counter++;
latest_input = input;
if (input < smallest) {
smallest = input;
}
if (input > largest) {
largest = input;
}
i++;
}
System.out.println("The largest integer is: " + largest);
System.out.println("The smallest integer is: " + smallest);
}
}
The HiLoFor program is:
import java.util.Scanner; // Import Scanner class to get user input
public class Main{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
int counter = 0;
int latest_input = 0;
int smallest = 0;
int largest = 0;
for (int i = 0; i < 10; i++) {
int input = scan.nextInt();
counter++;
latest_input = input;
if (input < smallest) {
smallest = input;
}
if (input > largest) {
largest = input;
}
}
System.out.println("The largest integer is: " + largest);
System.out.println("The smallest integer is: " + smallest);
}
}
The HiLoDoWhile program is:
import java.util.Scanner; // Import Scanner class to get user input
public class Main{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
int counter = 0;
int latest_input = 0;
int smallest = 0;
int largest = 0;
int i = 0;
do {
int input = scan.nextInt();
counter++;
latest_input = input;
if (input < smallest) {
smallest = input;
}
if (input > largest) {
largest = input;
}
i++;
} while (i < 10);
System.out.println("The largest integer is: " + largest);
System.out.println("The smallest integer is: " + smallest);
}
}
The psuedocode for HiLoWhile program is:
BEGIN
Counter = 0
Latest_input = 0
Smallest = 0
Largest = 0
i = 0
WHILE i < 10
Input_int = INPUT()
Counter++
Latest_input = Input_int
IF Input_int < Smallest THEN
Smallest = Input_int
END IF
IF Input_int > Largest THEN
Largest = Input_int
END IF
i++
PRINT("The largest integer is: ", Largest)
PRINT("The smallest integer is: ", Smallest)
END
I hope that answers all four parts of your question.