# Use a boolean to find if a number is divisible

• October 3rd, 2012, 12:51 PM
ColeTrain
Use a boolean to find if a number is divisible
i am making a program to see if a number is divisible by 5 and/or 6. how do i do this using a boolean? the user inputs and int and then i calculate it and tell them true and/or false. thanks
• October 3rd, 2012, 12:53 PM
KevinWorkman
Re: Use a boolean to find if a number is divisible
Which part of this is giving you trouble? Determining divisibility? Storing something in a variable? Using a boolean variable? Outputting a variable?
• October 3rd, 2012, 04:14 PM
ColeTrain
Re: Use a boolean to find if a number is divisible
well actually it doesnt have to be boolean i realized. I have no idea how to see if a number is divisible by another, so help needed there. But as for the data type i just need one that states true if it is divisible and false if it isnt. please help. thanks
• October 3rd, 2012, 04:29 PM
ColeTrain
Re: Use a boolean to find if a number is divisible
heres what i have so far... dont have the divisibility equation yet.

Code java:

```import java.util.Scanner;   public class Pg127 { public static void main(String[] args) {   //Input and output= Console   Scanner input= new Scanner(System.in);   System.out.println("Enter an interger: ");   String number=input.next();   int integer = Integer.parseInt(number);   if( integer 5 && integer 6){ System.out.println("Is " + integer + " divisible by 5 and 6? "); } else if(integer 5 || integer 6){ System.out.println("Is " + integer + " divisible by 5 or 6? "); } else if (integer 5 || integer 6 ){ System.out.println("Is " + integer + " divisible by 5 and 6, but not both? "); }     }   }```

also on the last one i have to make it ask if it is divisible by one or the other but not both... i know i have to add an && but how do i specify NOT
• October 3rd, 2012, 04:37 PM
ColeTrain
Re: Use a boolean to find if a number is divisible
nevermind, i actually ended up figuring it out.

Heres what I ended with:

Code java:

```import java.util.Scanner;   public class Pg127 {   public static void main(String[] args) {   //Input and output= Console   Scanner input = new Scanner(System.in);   System.out.println("Enter an interger: ");   String number = input.next();   int integer = Integer.parseInt(number);   if (integer % 5 == 0 && integer % 6 == 0) { System.out.println("Is " + integer + " divisible by 5 and 6? True"); } else { System.out.println("Is " + integer + " divisible by 5 and 6? False"); }     if (integer % 5 == 0 || integer % 6 == 0) { System.out.println("Is " + integer + " divisible by 5 or 6? True "); } else { System.out.println("Is " + integer + " divisible by 5 or 6? False"); }     if (integer % 5 == 0 && integer % 6 == 0) { System.out.println("Is " + integer + " divisible by 5 and 6, but not both? False "); } else { System.out.println("Is " + integer + " divisible by 5 or 6, but not both? True "); }     } }```

Thank you so much for responding though. I was just not using my brain earlier and made it harder than it should have been haha.
• October 3rd, 2012, 05:16 PM
jps
Re: Use a boolean to find if a number is divisible
It still appears more complicated than is necessary in my opinion.

For example I see:
integer % 5 == 0
and:
integer % 6 == 0
each three times in your code. This means you are doing the same work three times.

When I think of your problem, I see a solution in my head formed in the following layout:
program starts
user enters (valid) input
determine if input % 5 == 0
determine if input % 6 == 0

combine the results into a formatted output

display results