# prime number

Page 1 of 1

## 9 Replies - 3802 Views - Last Post: 04 November 2008 - 05:31 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=70381&amp;s=70e1d725e4567f9d443e4b3dd13d906b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ozyy

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 04-November 08

# prime number

Posted 04 November 2008 - 03:31 AM

a java program to get prime numbers between 1 and 10
Is This A Good Question/Topic? 0

## Replies To: prime number

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 6597
• Posts: 30,790
• Joined: 10-May 07

## Re: prime number

Posted 04 November 2008 - 03:32 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.

### #3 ozyy

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 04-November 08

## Re: prime number

Posted 04 November 2008 - 03:38 AM

public class Primeno{
public static void main(String[ ]args){
int m,i,k;
for(i= 1; i<10; i++){
m = i%i;
if (m==1);
System.out.print(m);
}
if(i ==9);
System.out.println();
}
}

### #4 no2pencil

• Professor Snuggly Pants

Reputation: 6597
• Posts: 30,790
• Joined: 10-May 07

## Re: prime number

Posted 04 November 2008 - 03:48 AM

** Topics Merged **

### #5 ozyy

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 04-November 08

## Re: prime number

Posted 04 November 2008 - 04:22 AM

public class Primeno{
public static void main(String[ ]args){
int m,i,k;
for(i= 1; i<10; i++){
m = i%2;
if (m==1);
System.out.print(m);
}
if(i ==9);
System.out.println();
}
}

### #6 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: prime number

Posted 04 November 2008 - 04:34 AM

That's not the way you find prime numbers. I think your logic will give you list of odd numbers and thus 9 will also be listed in your prime numbers list, which is wrong.

There are snippets available on this topic in snippets section. just search for them and you will get your answer.

### #7 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: prime number

Posted 04 November 2008 - 04:43 AM

You might like to merge this as well...
http://www.dreaminco...showtopic=70385

### #8 gabehabe

• GabehabeSwamp

Reputation: 1430
• Posts: 11,006
• Joined: 06-February 08

## Re: prime number

Posted 04 November 2008 - 05:25 AM

You're actually just limiting your chances of getting help by posting so many topics. Stop. Merged.

### #9 mostyfriedman

• The Algorithmi

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

## Re: prime number

Posted 04 November 2008 - 09:10 AM

a good way to find prime numbers is by checking odd numbers only, as all even numbers are prime except 2... so basically the numbers that are less than 2 are not prime, 2 is prime and the odd numbers greater than 2 maybe prime or not( which you will have to check)....this should help you solve your algorithm..good luck

### #10 pbl

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

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

## Re: prime number

Posted 04 November 2008 - 05:31 PM

mostyfriedman, on 4 Nov, 2008 - 08:10 AM, said:

a good way to find prime numbers is by checking odd numbers only, as all even numbers are prime except 2... so basically the numbers that are less than 2 are not prime, 2 is prime and the odd numbers greater than 2 maybe prime or not( which you will have to check)....this should help you solve your algorithm..good luck

and as you are doing it from only 1 to 10 no performance problem in making 2 loops checking with all the previous number no need to implement the algorith of ... don't remember his name of Erosthene