• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic