I am trying to write a program that displays the integers between 1 and 100 that are divisible by either 6 or 7 but not both.
Here is my code:
import acm.program.*;
public class Problem4 extends ConsoleProgram
{
public void run()
{
for (int i = 1; i <= 100; i++)
{
boolean num = ((i % 6 == 0) || (i % 7 == 0));
if (num == true)
println(i + " is divisible");
}
}
}
The above code shows the following answers: 6,7,12,14,18,21,24,28,30,35,36,42,48,49,54,56,60,63,66,70,72,77,78,84,90,91,96,98
Now the bold numbers 42 and 84 are both divisbile by 6 and 7. Now If I change the ||
to &&
in the above code, the result shows only 42 and 84.
What change should I do to remove these 2 numbers from the final result?
you have to make your condition look like:
boolean num = (i % 6 == 0 || i % 7 == 0) && !(i % 6 == 0 && i % 7 == 0);
that's basically converting "but not both" to Java code :)