FastFoodCoding

Login


Connect with one of the following accounts





Login

Please wait while we redirect...

Views: 236   



Java program which takes a number as input and prints its a Prime number or not


import java.util.Scanner;

public class PrimeNumber {

    public static void main(String[] args) {

        System.out.println("Please enter a number: ");

        Scanner in = new Scanner(System.in);
        int input = in.nextInt();
        in.close();

        boolean isPrimeNumber = true;
        for (int i = 2; i <= input - 1; i++) {
            if (input % i == 0) {
                isPrimeNumber = false;
            }
        }

        if (isPrimeNumber == true) {
            System.out.println("It's prime number");
        } else {
            System.out.println("It's not prime number");
        }

    }
}

Output:

Please enter a number: 
27
It's not prime number



On By


Top Tutorials

Top Questions

Top Articles

Top Blogs

Top News