Question

In: Computer Science

Write a program, ArrayRange, that asks the user to input integers and that displays the difference...

Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display all the numbers given by the user, the max number, the min number, and the difference. Java Programming

Output

How many integers will you enter? 6

Enter 6 integers: 7 15 9 4 12 17

You entered: 7 15 9 4 12 17

Min: 4

Max: 17

Range: 13

Solutions

Expert Solution

Java code:

import java.util.Scanner;
public class ArrayRange{
   public static void main(String[] args){
       Scanner input=new Scanner(System.in);
       //asking for n
       System.out.print("How many integers will you enter? ");
       //accepting it
       int n=input.nextInt();
       //asking for numbers
       System.out.print("Enter "+n+" integers: ");
       //array of list of numbers
int[] numbers=new int[n];
//looping from 0 to n
for(int i=0;i<n;i++){
//accepting number
numbers[i]=input.nextInt();
}
//printing the numbers
System.out.print("You entered: ");
//assigning first number as min
int min=numbers[0];
//assigning first number as max
int max=numbers[0];
//looping from 0 to n
for(int i=0;i<n;i++){
//checking if numbers[i] is greater than max
if(numbers[i]>max)
//assigning numbers[i] to max
max=numbers[i];
//checking if numbers[i] is less than min
if(numbers[i]<min)
//assigning numbers[i] to min
min=numbers[i];
System.out.print(numbers[i]+" ");
}
//printing newline
System.out.println();
//printing min
System.out.println("Min: "+min);
//printing max
System.out.println("Max: "+max);
//printing min
System.out.println("Range: "+(max-min));
  
   }
}


Screenshot:


Input and Output:


Related Solutions

The program asks user to enter three integers, and the displays a message indicating whether the...
The program asks user to enter three integers, and the displays a message indicating whether the numbers are in sequential order, in reverse order or in neither order. Find the errors and correct the program codes in the if comparison expressions. (10 points) 1.     using System; 2.     using static System.Console; 3. 4.     class Sorting 5.     { 6.              static void Main() 7.              { 8.                       int num1, num2, num3; 9.                       string snum1, snum2, snum3; 10.                    Write ("Enter first number "); 11.                    snum1...
Write a program that first asks the user to input 10 different integers a_1-a_10 and another...
Write a program that first asks the user to input 10 different integers a_1-a_10 and another integer b, then outputs all the pairs of the integers a_i and a_j satisfying a_i+a_j=b, i!=j. If there is no such pair, output “not found.”
Create a program that asks the user for two positive integers. If the user's input for...
Create a program that asks the user for two positive integers. If the user's input for either integer is not positive (less than or equal to 0), re-prompt the user until a positive integer is input. You may assume that all inputs will be integers. Print out a list, ascending from 1, of all divisors common to both integers. Then, print out a message stating whether or not the numbers are "relatively prime" numbers. Two positive integers are considered relatively...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
c program Write a program that asks the user to enter a sequence of 15 integers,...
c program Write a program that asks the user to enter a sequence of 15 integers, each either being 0, 1, or 2, and then prints the number of times the user has entered a "2" immediately following a "1". Arrays are not allowed to appear in your code. Include ONLY THE SCREENSHOT OF YOUR CODE in an image file and submit the file.
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
Write a program in C++ that asks the user for his/her Taxable Income and displays the...
Write a program in C++ that asks the user for his/her Taxable Income and displays the Income Tax owed. Use the following table to calculate the Income Tax: Taxable Income Tax Rate $0 to $9,225 / 10% $9,226 to $37,450 / $922.50 plus 15% of the amount over $9,225 $37,451 to $90,750 / $5,156.25 plus 25% of the amount over $37,450 $90,751 to $189,300 / $18,481.25 plus 28% of the amount over $90,750 $189,301 to $411,500 / $46,075.25 plus 33%...
Write a program compare.cpp that asks the user to input two dates (the beginning and the...
Write a program compare.cpp that asks the user to input two dates (the beginning and the end of the interval). The program should check each day in the interval and report which basin had higher elevation on that day by printing “East” or “West”, or print “Equal” if both basins are at the same level. Example: $ ./compare Enter starting date: 09/13/2018 Enter ending date: 09/17/2018 09/13/2018 West 09/14/2018 West 09/15/2018 West 09/16/2018 West 09/17/2018 West Explanation: Date East (ft)...
Write an assembly program that lets the user to input only the word MASM and displays...
Write an assembly program that lets the user to input only the word MASM and displays invalid input for any other user inputs.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT