The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

program to display first 100 prime nos.

Posted by swapnil on November 16, 2000 at 9:34 AM

hi,
pl. find below the program to display the first 100 prime nos.
.................................................................


//program that prints first 100 prime no.s
import java.util.*;
public class prime2
{
public static void main(String[] args)
{ int index=2,x=3;
ArrayList al=new ArrayList();
while(index<100)
{
//int x=Integer.parseInt(args[0]);
if(x%2==0)
{ System.out.println("hello The no. is not prime");
}
else
{
double y=Math.sqrt(x);
//System.out.println(y);
double z=Math.ceil(y);
//System.out.println(z);
boolean state=true;
for (int i=3;i { if(i%2==0 )
continue;
if(x%i==0)
{
//System.out.println("The no. is not prime");
//System.out.println("The no. is "+x);
//System.out.println(" It is divisible by "+i);
state=false;
//break;
}
}
if(state)
{
System.out.println("The no. is prime");

al.add(new Integer(x));
index=index+1;


}
x=x+2;
}
}

Object myobj[]=al.toArray();
for(int i=0;i System.out.println(myobj[i]+"\t"+i+"");


}
}
.................................................................

hope this helps
regards
swapnil



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us