Why the program gives wrong output?
-
21-12-2019 - |
Question
BigInteger p = new BigInteger("1");
BigInteger m = new BigInteger("1");
BigInteger j = new BigInteger("1");
BigInteger n = new BigInteger("3");
BigInteger one = new BigInteger("1");
while (m.compareTo(n) == -1) {
while (j.compareTo(n) == -1) {
p = m.multiply(j);
System.out.println("m=" + m + " j=" + j + " p=" + p);
j = j.add(one);
}
m = m.add(one);
}
Why the output is
m=1 j=1 p=1
m=1 j=2 p=2
Shouldn't it be
m=1 j=1 p=1
m=1 j=2 p=2
m=2 j=1 p=1
m=2 j=2 p=2
?
Solution
No. Why would j
revert from 2 to 1? You are only ever adding to it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow