VMoza - 6 months ago 34

Java Question

The question is exactly as follows:

Write a method that determines whether a number is prime.

Then use this method to write an application that determines and displays all the prime numbers less than 10,000.

I have already written a program that finds all prime numbers up to 10,000, but then found a simpler and more efficient one on StackOverflow, and that is this:

`package prime;`

import java.util.Scanner;

public class Prime

{

public static void main(String[] args)

{

for(int i = 1; i <= 10000; i++)

{

int factors = 0;

int j = 1;

while(j <= i)

{

if(i % j == 0)

{

factors++;

}

j++;

}

if (factors == 2)

{

System.out.println(i);

}

}

}

}

Since I am very new to Java and am especially not good at methods, this problem is especially difficult for me. I tried making a method, but nothing is being returned, and when I try to return something I get error after error after error.

The help I need is mainly just Pseudo-Code for what I should do to tackle this problem; I'm not asking you for the answer, I'm just asking for a start.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
package prime;
public class Prime
{
public static void main(String[] args)
{
for(int i = 1; i <= 10000; i++)
{
if (isPrimeNumber(i))
{
System.out.println(i);
}
}
}
public static boolean isPrimeNumber(int i) {
int factors = 0;
int j = 1;
while(j <= i)
{
if(i % j == 0)
{
factors++;
}
j++;
}
return (factors == 2);
}
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**