Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Use a boolean to find if a number is divisible

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Use a boolean to find if a number is divisible

    heres what i have so far... dont have the divisibility equation yet.

    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

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Use a boolean to find if a number is divisible

    nevermind, i actually ended up figuring it out.

    Heres what I ended with:

    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.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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

Similar Threads

  1. [SOLVED] how to find all occurrences of a number in an array recursivelly
    By mia_tech in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 13th, 2012, 01:37 PM
  2. Find Biggest Number in Array
    By jo15765 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 12th, 2012, 03:01 PM
  3. Find the two largest number
    By vendettabf in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 29th, 2011, 12:23 PM
  4. find the sum of even number not exceed four million
    By i4ba1 in forum Algorithms & Recursion
    Replies: 10
    Last Post: June 29th, 2011, 08:08 AM
  5. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 12:46 PM