Question

Using java prompt the user to enter an integer called n. Then scan in n values...

Using java prompt the user to enter an integer called n. Then scan in n values of type double.Find the middle value of the n numbers using a for loop. Assume the middle value is the ((n+1)/2) th number entered.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//MiddleNumber.java
import java.util.Scanner;
public class MiddleNumber {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number: ");
        int n = scan.nextInt();
        double result = -1, k;

        System.out.println("Enter "+n+" double values");
        for(int i = 0;i<n;i++){
            k = scan.nextDouble();
            if((i+1)==((n+1)/2)){
                result = k;
            }
        }

        System.out.println("Middle value: "+result);
    }
}

Add a comment
Know the answer?
Add Answer to:
Using java prompt the user to enter an integer called n. Then scan in n values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java Write an application called Squaring that will: • Prompt the user to enter an...

    in java Write an application called Squaring that will: • Prompt the user to enter an integer greater than 1. Your code for this should be type-safe and validate the number, meaning that your code should re-prompt the user for another entry if they enter anything other than an integer that is greater than 1. • Repeatedly square the integer until it exceeds 1 million, outputting the value each time after squaring. • Output the number of squarings required to...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • In Java please: Write a program that will prompt the user to enter GPA values one...

    In Java please: Write a program that will prompt the user to enter GPA values one per line, stopping when the user enters a negative Input: value. Print the following on separate lines: . The number of vaild GPAs entered » The sum of the GPAs 4.0 3.7 2.9 3.5 -1 The average GPA

  • Write a while loop in java that will let the user enter a series of integer...

    Write a while loop in java 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.

  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

  • Write a program that will loop ten times. In each iteration prompt user to enter an...

    Write a program that will loop ten times. In each iteration prompt user to enter an integer between -50&50. Print the input, keep a running sum of the inputs, and track the minimun and maximum numbers input. After the loop is complete, minimum number entered, maximum number entered, sum of all numberes entered, and average of all numbers entered. Format all output. Before running the loop, print the label "Input values: ". Within the loop, print value entered by user...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Q2. To check and print if entered integer is prime or not. Prompt the user to...

    Q2. To check and print if entered integer is prime or not. Prompt the user to enter a positive integer or 0 to exit. while negative integer entered - prompt for valid values again. If positive integer entered - check if it is prime. Write a separate function that accepts a positive integer as argument and checks and returns 1 if the integer is prime or 0 if it is not prime. Call the function from main. Accept the return...

  • In Java Programming: Create a flow chart for a program that prompts for an integer N...

    In Java Programming: Create a flow chart for a program that prompts for an integer N (number of students) and M (number of test scores for each students), and allows the user to N*M real numbers. The program then calculates the average for each student and class average. (1) Prompt the user to input N students and M test scores, then display the inputs. Enter number of students: You entered: 2 Enter number of test scores: You entered: 3 Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT