Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Finding prime numbers

807607Oct 25 2006 — edited Oct 25 2006
Hello,

I am currently taking an intro to java class and i was looking for help on the following problem.
I need to find all the prime numbers before N than display them along with how many prime numbers there are.
I cant put a counter in the loop since that will be equal to i and anything i put in the method will be reset. I have tried a few diffrent things yet no result
Any help that could point me in the right direction would be great.
Thank you

Here is my code so far. This will get all the prime number before the value N but i need to come up with a way of finding out how many prime numbers are before N.
import java.util.*;
public class Prime {


public static void Primes(int x){
if ((x%2)==0 && x!= 2){
System.out.print("");
}
else if((x%3)==0 && x != 3){

System.out.print("");
}
else if ((x%5)==0 && x != 5){
System.out.print("");
}
else{
System.out.print(+ x + " ");
}
}



public static void main(String [] args){
int repeatseq;
int num = 0;
Scanner value = new Scanner(System.in);
System.out.println("I want all the prime numbers before ");
repeatseq = value.nextInt();
for (int i = 0; i < repeatseq; i++){
Primes(i);
}

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 22 2006
Added on Oct 25 2006
7 comments
404 views