Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Prime Nos

  Asked By: Sean    Date: Jul 30    Category: Java    Views: 1186
  

This is a very basic question yet can somebody please tell me a way
to
1) Generate prime nos
2) check if a no is prime or not

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Becky Baker     Answered On: Jul 30

roughly...

int range = 100;
boolean prime  = true;

for(int i=0; i!=range; i++)
{
for (int j=0; j!=i; j++)
{
if ( i mod j == 0 )
prime=false;
break;
}

if(prime)
{
// caught prime number
}

}

basically though, it is better to store found prime numbers in an
ArrayList. Then you can check  prime numbers in the array list, and
then those numbers that are larger than the largest known prime
number.

To find if a number is prime, use the inner for loop.

 
Didn't find what you were looking for? Find more on Prime Nos Or get search suggestion and latest updates.




Tagged: