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
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.
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
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!
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
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
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.
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