Question

In: Computer Science

Write a Java program that uses a while loop to do the following: Repeatedly asks the...

  1. Write a Java program that uses a while loop to do the following:
    • Repeatedly asks the user to enter a number or -1 to exit the program.
    • Keeps track of the smallest and largest numbers entered so far:
      • You will need a variable for the smallest number and another variable for the largest number.
      • Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number.
      • Use a loop to gather the remaining input. Each time the user enters a new number that is not -1, compare it to the variables and update them if necessary.
    • When the user enters -1, display the smallest and largest numbers that the user entered. If the user did not enter any numbers before entering -1, then simply exit the program.
  2. Run the program and test it on several different sets of values to see if it works.
  3. Modify the program to ask the user how many numbers they wish to enter and use a for loop to ask for that many numbers. The sentinel value of -1 should no longer be necessary.
  4. Run the program and test it on several different sets of values to see if it works.
  5. Here is a sample output of the program

How many numbers do you wish to enter? 5

Please enter a number: 8

Please enter a number: 9

Please enter a number: 12

Please enter a number: 15

Please enter a number: 3

The minimum number is 3 and the maximum number is 15.

Solutions

Expert Solution

CODE IN JAVA:

import java.util. * ;
public class main {
  
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       int min = 999999, max = -99999;
       System.out.print("How many numbers do you wish to enter? ");
       int n = sc.nextInt();
       for(int i = 1; i <= n; i++) {
           System.out.print("Please Enter a number: ");
           int number = sc.nextInt();
           if(number > max)
               max = number ;
           if(number < min)
               min = number ;
       }
       System.out.println("The minimum number is " + min + " and the maximum number is " + max + ".");
   }

}

OUTPUT:


Related Solutions

Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT