Bogdan Dragan - 1 year ago 62

Java Question

Ok so i made this program that verify if a number is prime.I can't figure out how to make a program that can verify that all numbers < n are prime.Can you help me ?

`import javax.swing.*;`

public class Main {

public static void main(String[] args) {

int i;

int n;

boolean nPrime=true;

n = Integer.parseInt(JOptionPane.showInputDialog("Entrer un numero"));

for (i = 2; i < n; i++) {

if (n % i == 0) {

nPrime = false;

}

}

if (nPrime) {

System.out.println("Le numero " + n + " est prime");

} else

System.out.println("Le numero " + n + " n'est pas prime");

}

}

Answer Source

I will try to answer your question by not changing your code very much.

```
import javax.swing.*;
public class Main {
public static void main(String[] args)
{
int i;
int n; //here is your number (say 20) and you need all primes below 20
int number; // it will loop through all numbers between 2 to n
boolean nPrime=true;
n = Integer.parseInt(JOptionPane.showInputDialog("Entrer un numero"));
for ( number = 2 ; number <= n ; ++number ) // for outer
{
for (i = 2; i < number; i++) //for inner
/*from here it is simply your code which checks every number
for primeness, BTW you need to check for only upto
square_root(number) so change your code to i*i < number, I
didn't changed it just to make it as similar to yours as
possible
*/
{
if (number % i == 0)
{
nPrime = false;
}
}//for inner closed
if (nPrime)
{
System.out.println("Le numero " + number + " est prime");
}
else
System.out.println("Le numero " + number + " n'est pas prime");
//if you need only primes then remove this else part
nPrime=true;
}//for outer closed
}// main() closed
}//class closed
```