if((x % 2)> 0)
is not condition for checking prime. It would given remainder after division, here telling if its odd or even. Being just odd is not condition for prime number as it might have other odd factors too for eg. 21 = 7 * 3 .
Here is simple java code to get all prime numbers between 0 - 100. Explanation mentioned in comments
class Main {
public static void main(String[] args) {
for (int n = 2; n <= 100; n++) { // loop for all numbers
boolean flag = true; // to check if factors other than 1, n
for (int f = 2; f <= Math.sqrt(n); f++) {
if (n % f == 0) { // factor found, hence not prime
flag = false; // activate flag
break; // break
}
}
if (flag) { // if flag is not re set
System.out.print(n + ", "); // print n
}
}
}
}
Output :
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
If you are looking for efficiency as well, here is implementation of Sieve of Eratosthenes
class Main {
public static void main(String[] args) {
int N = 100;
// Array to keep record of primes and non primes,
// 0 - prime, 1 - composite
int[] arr = new int[N + 1];
arr[0] = arr[1] = 1;
// loop from 2 to square root of N
for (int i = 2; i <= Math.sqrt(N); i++) {
// check if currently prime
if (arr[i] == 0) {
// add all factors of i to composite numbers upto N
for (int j = i * i; j <= N; j += i) {
arr[j] = 1;
}
}
}
// Print all positions for which fields are not reset
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
System.out.print(i + ", ");
}
}
}
}
Output :
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,