programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

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

Ranch Hand
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 ]

Ranch Hand
Posts: 1067
2
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.

lowercase baba
Posts: 12871
62
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 ]

 Don't get me started about those stupid light bulbs.