public static void sumOfSquareRoots() {
        Scanner scan = new Scanner( System.in );
        double value;

        // read in a double
        System.out.print  ("Enter a double:");
        value = scan.nextDouble();

        // calculate its square root
        double result = Math.sqrt( value );

        // write out the result
        System.out.println("square root of your number is " + result );
        System.out.println("The sum of square root of numbers is"+result);
      }

"This calculates the square root of a given number how do I add multiple numbers and add the sum of the square roots ?

Dani AI

Generated

Nice start, . To get a sum of square roots you just need to loop, take each input, add Math.sqrt(value) to a running total, and stop when the user is done. is right about using a loop with either a known count or a sentinel value, and ’s hasNextDouble() tip lets users paste many numbers at once.

Known count (ask how many values first):

public static void sumOfSquareRoots() {
    Scanner scan = new Scanner(System.in);
    System.out.print("How many numbers? ");
    int n = scan.nextInt();

    double sum = 0.0;
    for (int i = 0; i < n; i++) {
        System.out.print("Enter a non-negative double: ");
        double v = scan.nextDouble();
        if (v < 0) {
            System.out.println("Skipping negative: " + v);
            continue; // or handle complex numbers if needed
        }
        sum += Math.sqrt(v);
    }
    System.out.printf("Sum of square roots = %.6f%n", sum);
    // Do not close the scanner if it wraps System.in
}

Unknown count (type done to finish). This uses ’s idea so users can enter many numbers separated by spaces or newlines:

public static void sumOfSquareRoots() {
    Scanner scan = new Scanner(System.in);
    double sum = 0.0;
    System.out.println("Enter numbers (type 'done' to finish):");
    while (true) {
        if (scan.hasNextDouble()) {
            double v = scan.nextDouble();
            if (v < 0) {
                System.out.println("Skipping negative: " + v);
                continue;
            }
            sum += Math.sqrt(v);
        } else {
            String token = scan.next();
            if (token.equalsIgnoreCase("done")) break;
            System.out.println("Not a number, try again.");
        }
    }
    System.out.printf("Sum of square roots = %.6f%n", sum);
}

Notes:

  • Math.sqrt of a negative is NaN; decide whether to skip or reject those inputs.
  • Prefer printf for clean formatting.
  • If you later read from System.in again, avoid scan.close() to keep the stream open.

Recommended Answers

All 2 Replies

how do I add multiple numbers

Make a loop that asks for a number, reads the number, does a computation with the number
and then loops back to the top where it asks for the number again.
To end the loop either ask the user before entering the loop for the number of inputs that will be made OR have a special number(like 0) that the user enters that tells the program to exit the loop.

In the scanner object,use a while loop with the condition hasNextDouble() method.By this the user can input any number of doubles at a single go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.