Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

prime number problem  RSS feed

 
sreedhara satuluri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
i will be grateful if any one can send me a program that generates a prime number upto 500 and i need to pick up a single prime number from it...


It is a bit urgent and i need it by today evening.....
please help me out
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you pls show us what code you'v got so far?
 
Michael Dunn
Ranch Hand
Posts: 4632
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Follow a stepwise approach:
1> first decide the logic u want to use to detect if a number is prime or not
2> write a for loop for 0 to 500 and check if each number is prime or not

3> Start coding with whatever you know

4> Now post your doubts here. You will surely get a good response.
 
lalit upadheyay
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sreedhara satuluri

You asked an easy logical question. The simple straight answer is --

class PrimeGenerator500
{
public static void main(String []args)
{
for(int i=2;i<=500;i++)
{
boolean flag=true;
for(int j=2,k;j<=i/2;j++)
{
if(i>2)
{k=i%j;
if(k==0)
{
flag=false;
break;
}
}
}
if(flag)
System.out.println(i);
}
}
}
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by lalit upadheyay:
Hi sreedhara satuluri
You asked an easy logical question. The simple straight answer is --


Lalit,

Around here, the previous replies to the original post (OP) are customary for solicitations for solutions to homework/school assignments. We prefer to help folks learn to do things themselves and perhaps learn something in the process. We believe that giving out solutions the way you just did is counter-productive and detrimental to the OP.
 
lalit upadheyay
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu! i am sorry , i was just trying to help but i hadn't realized till my last reply that the imapct would be bad. Thanks for the advice. I would take care of ur advice in my future postings.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!