prime number

a java program to get prime numbers between 1 and 10

Page 1 of 1

9 Replies - 2974 Views - Last Post: 04 November 2008 - 05:31 PM Rate Topic: -----

#1 ozyy  Icon User is offline

  • New D.I.C Head

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

prime number

Post icon  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  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,879
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 ozyy  Icon User is offline

  • New D.I.C Head

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

Re: prime number

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


Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,879
  • Joined: 10-May 07

Re: prime number

Posted 04 November 2008 - 03:48 AM

** Topics Merged **
Was This Post Helpful? 0
  • +
  • -

#5 ozyy  Icon User is offline

  • New D.I.C Head

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

Re: prime number

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


Was This Post Helpful? 0
  • +
  • -

#6 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: prime number

Posted 04 November 2008 - 05:31 PM

View Postmostyfriedman, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1