public class Main {
public static void main(String[] args) {
long product = 1L;
product = (9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9);
System.out.println(product);
product = 9L;
for (int i = 0; i != 12; i++) {
product *= 9;
}
System.out.println(product);
}
}
Output :-754810903
2541865828329 //this one is correct
In your first attempt, you don't make sure the result is long
. Therefore - it overflows as int
.
product = (9L * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With