#JAVA While code

1 messages · Page 1 of 1 (latest)

late lion
#

Hello everyone,

When I went through this code, I thought the result will be 24 but was not the case. I am getting confused with the c = c+ b *(a%2); I tried to solve it below but I am getting stuck, can you maybe help? Where is the mistake, that I making?

int a = 10;
int b = 15;
int c = 0;

    do {
        c += b * (a % 2);
        // c = c + b * (a%2); // 0 = 0 + 15 * (10%2) // c = 15 * 5 = 75 IT1
        //c = c + b * (a%2); // 0 = 0 + 15 (11%2)

        if ( a % 2 == 0) { //10%2 == 0 true IT1
            ++a; //11 IT1
        }
        else {
            a = a + 2;
        }
    } while(a <= b); // 11 <= 75 true IT1

    System.out.println(c);
}
b += 1;

}

if ((a < b) && (a + c > b)) {
System.out.print(1 + " ");
}
else {
System.out.print(2 + " ");
}

if (a < c) {
System.out.print(3 + " ");
}

else {
System.out.print(4 + " ");
if (a + c + 1 > b) {
System.out.print(5+ " ");
}
}

tender meadowBOT
#

Hey, @late lion!
Please remember to /close this post once your question has been answered!

tender meadowBOT
#
Hello everyone,

When I went through this code, I thought the result will be 24 but was not the case. I am getting confused with the c = c+ b *(a%2);  I tried to solve it below but I am getting stuck, can you maybe help? Where is the mistake, that I making?

 int a = 10;
        int b = 15;
        int c = 0;

        do {
            c += b * (a % 2);
            // c = c + b * (a%2); // 0 = 0 + 15 * (10%2) // c = 15 * 5 = 75 IT1
            //c = c + b * (a%2); // 0 = 0 + 15 (11%2)

            if ( a % 2 == 0) { //10%2 == 0 true IT1
                ++a; //11 IT1
            }
            else {
                a = a + 2;
            }
        } while(a <= b); // 11 <= 75 true IT1

        System.out.println(c);
    }
    b += 1;
}

if ((a < b) && (a + c > b)) {
    System.out.print(1 + " ");
}
else {
    System.out.print(2 + " ");
} 

if (a < c) {
    System.out.print(3 + " ");
}

else {
    System.out.print(4 + " ");
    if (a + c + 1 > b) {
        System.out.print(5+ " ");
    }
}
late lion
#

Can please someone answer?

sturdy sail
#

try using a debugger

#

Debuggers let you go through your code line by line to see what's happening

spiral galleon
tender meadowBOT
late lion