Check if a number is Prime or not simple Java programming Code

 import java.util.Scanner;

public class PrimeNumberCheck {

    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();


        boolean isPrime = true;

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

            if (n % i == 0) { // n is multiple of i (i not equal to 1 or n)

                isPrime = false;

            }

        }

        if (isPrime == true) {

            System.out.print(n + " is prime number");

        } else {

            System.out.print(n + " is not prime number");

        }

    }

}

import java.util.Scanner;

public class PrimeNumberCheck {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        boolean isPrime = true;
        for (int i = 2; i <= n - 1; i++) {
            if (n % i == 0) { // n is multiple of i (i not equal to 1 or n)
                isPrime = false;
            }
        }
        if (isPrime == true) {
            System.out.print(n + " is prime number");
        } else {
            System.out.print(n + " is not prime number");
        }
    }
}

No comments:

Post a Comment

Max Sub Array Sum: Kadane's Algorithm

Code of: Max Sub Array Sum: Kadane's Algorithm. public class MaxSubArraySumKadanesALgorithm {     public static void Kadane ( int ...