Yes. Call the method println from System.out (not String.out which doesn't exist), you don't assign a value to it. And you multiply the int you parsed.
String.out.println = mataintal * mataintal;
should be
System.out.println(nr * nr);
Answer from Elliott Frisch on Stack OverflowYes. Call the method println from System.out (not String.out which doesn't exist), you don't assign a value to it. And you multiply the int you parsed.
String.out.println = mataintal * mataintal;
should be
System.out.println(nr * nr);
this makes no sense>
String.out.println = mataintal * mataintal;
you print using
System.out.println();
on the other hand, mataintal is a String, you need instead nr which is the result of parsing that string into an integer
your final line must be like:
System.out.println(nr * nr);
[Java] How do I find the square root of a number then round it up/down
[Java] Square every digit of a number?[Code Explanation]
Videos
The fastest way to square a number is to multiply it by itself.
Why is
Math.powso slow?
It's really not, but it is performing exponentiation instead of simple multiplication.
and why does it cope badly with > 1 and even worse with < -1 numbers
First, because it does the math. From the Javadoc it also contains tests for many corner cases. Finally, I would not rely too much on your micro-benchmark.
Squaring by multipling with self is the fastest. Because that approch can be directly translated into simple, non-branching bytecode (and thus, indirectly, machine code).
Math.pow() is a quite complex function that comes with various guarantees for edge cases. And it need to be called instead of being inlined.
This is probably a simple thing to most but Iโm just struggling to print the square root of a number then rounding it up or down in Java. Any help would be greatly appreciated!
public int squareDigits(int n) {
String result = "";
while (n != 0) {
int digit = n % 10 ;
result = digit*digit + result ;
n /= 10 ;
}
return Integer.parseInt(result) ;
}The above code returns a square of every digit like 99= 8181
I don't understand this code particularly String result = ""; n /= 10 ; and result = digit*digit + result ;
Can Someone Explain? Thanks.