# java program to calculate prime numbers

Page 1 of 1

## 5 Replies - 14862 Views - Last Post: 12 January 2009 - 06:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=80510&amp;s=14dd939449b1c753d2bfc99475261d2f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 runner03

Reputation: 0
• Posts: 2
• Joined: 12-January 09

# java program to calculate prime numbers

Posted 12 January 2009 - 01:31 PM

```Scanner keyboard = new Scanner(System.in);
int userNum;
int remander = 0;
System.out.print ("Enter a positive integer and find out all the prime numbers leading up to the number you chose: ");
userNum = keyboard.nextInt();

for(int lcv=2; lcv <= userNum; lcv++){

for (int lcv2=2;lcv2<=lcv-1;lcv2++){
//IF STATEMENT
if ((lcv2>=2)&&(lcv2<lcv)){
remander=(lcv % lcv2);
}

if (remander !=0){
System.out.print(lcv2);
}
}
}
}                                  // i need to calculate all the prime numbers between 2 and whatever the user inputs\
```

Mod Edit: Please use code tags when posting your code. Code tags are used like so =>

Thanks,
PsychoCoder

This post has been edited by PsychoCoder: 12 January 2009 - 01:36 PM

Is This A Good Question/Topic? 0

## Replies To: java program to calculate prime numbers

### #2 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

## Re: java program to calculate prime numbers

Posted 12 January 2009 - 01:37 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

• Post the code you're having problems with (DONE)
• Post the exact error you're receiving, if you are receiving one
• If no error explain what the code is doing versus what you want it to do
• Post your question in the body of your post, not the description field (or as a comment in your code)

### #3 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: java program to calculate prime numbers

Posted 12 January 2009 - 01:53 PM

check out the sieve of eratosthenes algorithm, its the fastest algorithm to generate prime numbers and its also really simple as well.. i think you might find it in the snippets section, if not then you can google it

### #4 runner03

Reputation: 0
• Posts: 2
• Joined: 12-January 09

## Re: java program to calculate prime numbers

Posted 12 January 2009 - 05:01 PM

PsychoCoder, on 12 Jan, 2009 - 12:37 PM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
• Post the code you're having problems with (DONE)
• Post the exact error you're receiving, if you are receiving one
• If no error explain what the code is doing versus what you want it to do
• Post your question in the body of your post, not the description field (or as a comment in your code)

PsychoCoder, on 12 Jan, 2009 - 12:37 PM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
• Post the code you're having problems with (DONE)
• Post the exact error you're receiving, if you are receiving one
• If no error explain what the code is doing versus what you want it to do
• Post your question in the body of your post, not the description field (or as a comment in your code)

I do not get any syntax errors just logic errors.... I have too for loops the first one just a counter from 2 to userInput.. the second for loop is a second counter from 2 to the first counter.. i believe where my problems are, is in the if statement inside of the for loop...i cant figure out what that should be.. and my program is in no way printing the prime numbers

This post has been edited by runner03: 12 January 2009 - 04:59 PM

### #5 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: java program to calculate prime numbers

Posted 12 January 2009 - 05:09 PM

Why do you have nested for loop to print out the prime numbers?

Just have one loop which loops from 2 until the user entered number and call a method to check if that number is prime, if it is print if not don't print.

Or else see: Sieve of Eratosthenes as mentioned above to calculate the prime numbers in your list

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: java program to calculate prime numbers

Posted 12 January 2009 - 06:25 PM

for performance reason you can have your loop of lcv2 go to sqrt(lcv) not lcv-1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }