isPrime

mail@pastecode.io avatar
unknown
java
a year ago
1.2 kB
5
Indexable
Never
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        ArrayList<Integer> prime = new ArrayList<>();
        boolean isPrime = true;
        // Find prime numbers in this range

        for (int i = 1; i <= n; i++) {

            // Skip 0 and 1 as they are
            // neither prime nor composite
            if (i == 1 || i == 0)
                continue;

            // flag variable to tell
            // if i is prime or not
            isPrime = true;

            for (int j = 2; j <= i / 2; ++j) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }

            if (isPrime) {
                prime.add(i);
            }
        }
        int z = prime.size();

        for (int i = 0; i < z; i++) {
            for (int j = i; j < z; j++) {
                if(prime.get(i)+prime.get(j) == n)
                    System.out.println(prime.get(i) + " + " + prime.get(j) + " = " + n);
            }
        }
    }
}