File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Loops and prime numbers (Help me, I'm desperate!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loops and prime numbers (Help me, I Watch "Loops and prime numbers (Help me, I New topic

Loops and prime numbers (Help me, I'm desperate!)

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
I have to write a java program that takes a positive number entered by the user and displays whether or not it is prime. The assignment states that an even number is prime if it is 2, and an odd number is prime if it isn't divisible by any integer between 2 and the square root of the number. I really hadn't the faintest clue where to start, but I took a stab at it. Here is the program that I came up with...
Any help would be greatly appreciated as I haven't the faintest clue how to do this and the assignment is due tomorrow..

[ November 02, 2003: Message edited by: Brandi Love ]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

First, I don't think you want the prompt for the user to be inside the loop. It gets in the way of trying to figure out if you have a prime.
Next you are incrementing "count" in two places. You are going to confuse your self doing that.
You didn't add the quick and easy check for the user entering "2".
Don't worry about trying to loop to ask the user for another number. Get the program to work for the user entering one number. Than you will find it is easy to put all into a loop.
You are off to a good start. Add some print statements so that you can see what is happening and start testing with easy ones.

Please ignore post, I have no idea what I am talking about.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

once you have the users number, you need a loop to test it.

now, you can test isprime to see if it's prime or not
[ November 02, 2003: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Loops and prime numbers (Help me, I'm desperate!)
It's not a secret anymore!