how to display prime numbers in java>

Recommended Answers

All 5 Replies

follow these steps:
1. let say you need to findout all the primes till number n
2. create a outer for loop which will iterate from 1 to n (let say int i)
3. create a inner for loop(int j) and chk i%j
4. for prime numbers i%j will be 0

Or

private boolean is_prime(int n) {
for(int i=2; n<i*i; i++) {
if(i*i==n) {
return true;
}
/*
or
if(n%i==0) {
return true;
}
*/
}
return false; 
}

/* main */
int num = 123;
for(int i=2; i<num; i++) {
if(is_prime(i)==true) {
System.out.println(i);
}
}

But this code is not tested.

Or

...

But this code is not tested.

don't just hand out code. the purpose of this forum is to help him learn the language, not to do his work for him and help him cheat his way through college

And if they did use that code they won't be graduating from any college soon. It doesn't do a whole lot of much.

maybe, but doens't change the fact that a lot of 'students' are willing to pass with the minimum required grades

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.