Got my head in spaghetti mode.
Here is the question:
(Check a number) Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 3 and 7, or by neither of them, or by just one of them. Here are some sample runs for inputs, 9,21, and 25.
9 is divisible by 3 or 7, but not both 21 is divisible by both 3 and 7 25 is not divisible by either 3 or 7/
This is what I have so far. I know I'm wrong but do not think I am too far from solving the question.
public class Quest12 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int i = scan.nextInt();
if (i % 3 == 0 ^ 7 == 0) {
System.out.println(i + " is divisible by 3 or 7. ");
}
else if (i % 3 == 0 || 7 == 0)
{
System.out.println(i + " is divisble by either 3 or 7. but not both ");
}
if (i % 3 == 0 && 7 == 0)
{
System.out.println(i + " is divisble by both 3 and 7 ");
}
}
}
I would perform each modulus and store the result(s) in boolean
variables. Like,
boolean mod3 = i % 3 == 0;
boolean mod7 = i % 7 == 0;
if (mod3 && mod7) {
System.out.printf("%d is divisible by 3 and 7.%n", i);
} else if (mod3 || mod7) {
System.out.printf("%d is divisible by 3 or 7 (but not both).%n", i);
} else {
System.out.printf("%d is not divisible by 3 or 7.%n", i);
}