I am currently learning the basic math functions and I am struggling to get this output for my homework. I think the wording is just making me more and more confused. Thanks in advance for your help! Here is the problem layout:
Z = 7
Use the Math methods and the System.out.println statement to display:
- the square root of z squared plus 1
- Output desired: 7.0710678118654755
- My output: 8
My code: public class tester {
public static void main(String[] args) { int z = 7; System.out.println(Math.pow(Math.sqrt(z), 2) + 1); }
}
Advertisement
Answer
“the square root of z squared plus 1” means √(z2+1).
You are currently calculating (√z)2+1 — which incidentally is the same as z+1. Hence why you’re getting 8.