I created a method to run through an array add the numbers together in the array and then find the square root of the total. But when i checked the square root of the total on a calculator I got an entirely different answer and I was concerned.
The answer I got on the calculator was 3.16227766017. When I ran the code the answer I got was 2.4850206733870595
I am a beginner to coding but I wanted to make sure I did not make an error somewhere in my code. Code is as follows:
public static void main(String[] args) { double array1[] = {1,2,3,4}; double total = 0; for(double x : array1){ total += x; total = Math.sqrt(total); } System.out.println(total); } run:
2.4850206733870595 BUILD SUCCESSFUL (total time: 2 seconds)