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 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 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.
Write a program that asks the user to input a set of floating-point values. When the...
Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.5 pts Your code with comments A screenshot of the execution Test Case:       Enter float: 1.0...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
Question 1: 5pts Write a program to receive two integers as user input and then print:...
Question 1: 5pts Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: 10pts An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is...
Question 1: Write a program to receive two integers as user input and then print: Sum...
Question 1: Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is compounded monthly....
Write a C++ program using separate void which asks the user to input side of a...
Write a C++ program using separate void which asks the user to input side of a square, radius of a circle , height and base of a triangle and finds the area of squares, circles and triangles. Then using main function display the area of square, circle and triangle
In Java:Implement a program that repeatedly asks the user to input apositive integer and...
In Java:Implement a program that repeatedly asks the user to input a positive integer and outputs the factorial of that input integer. Do not use recursion, solution should use stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT