Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## prime numbers

So I understand the basics of finding prime numbers, but I'm trying to do something a bit different. I want to find the prime number that follows or comes before any random number. So for example if I picked 12, the prime numbers would be 13 and 11. I've thought about using a loop for it, but I'm not quite sure. Any ideas?

2. ## Re: prime numbers

What have you done so far? Do you know how to generate a random number? Do you have a method which generates all prime numbers?

3. ## Re: prime numbers

Yeah so far I only have entering the random number part.

```import java.util.Scanner;
public class prog335t
{
public static void main(String[]args)
{

Scanner input=new Scanner(System.in);
int n;
System.out.print("Enter an integer greater than or equal to 2: ");

n= input.nextInt();```

4. ## Re: prime numbers

For future reference, please surround your code with either the [highlight=java][/highlight] or Y[code][/code] tags (makes it much more readable). I've edited your post above.

First thing is first, you mentioned in your post above you understand the basics of finding prime numbers....do you have any code which finds prime numbers, or validates a number for being prime? Validating is important, for example a very simple brute-force algorithm to solve the original problem would be to continually increment/decrement the entered value - validating each resulting number until a number validates as being prime.

5. ## Re: prime numbers

this is what you looking for Sieve of Eratosthenes