anik_s
anik_s

Reputation: 263

Why the program gives wrong output?

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  

?

Upvotes: 0

Views: 60

Answers (1)

Graham Borland
Graham Borland

Reputation: 60691

No. Why would j revert from 2 to 1? You are only ever adding to it.

Upvotes: 3

Related Questions