a java program to get prime numbers between 1 and 10

## 9 Replies - 3668 Views - Last Post: 04 November 2008 - 05:31 PM

##
**Replies To:** prime number

### #2

## 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.

Please post like this:

Thank you for helping us helping you.

### #3

## 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(); } }

### #5

## 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

## 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.

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

### #7

## Re: prime number

Posted 04 November 2008 - 04:43 AM

### #8

## 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

## 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

## 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

Page 1 of 1