Different ways to Check Whether a Number is Prime or Not

Category : Java | Sub Category : Java Programs | By Prasad Bonam Last updated: 2020-10-03 14:28:09 Viewed : 921


Different ways to  Check Whether a Number is Prime or Not 

java.lang.System

 
The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
java.util.Scanner
 
A simple text scanner which can parse primitive types and strings using regular expressions.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
For example, this code allows a user to read a number from System.in:

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

 

 1. Check Prime Number using a for loop

PrimeNumberForLoop.java

package runnerdev;

 

import java.util.Scanner;

 

public class PrimeNumberForLoop {

 

            public static void main(String[] args) {

                        System.out.print("Enter a number to check prime : ");

                        Scanner sc = new Scanner(System.in);

                        int givenNumber = sc.nextInt();

 

                        boolean flag = false;

                        for (int i = 2; i <= givenNumber / 2; ++i) {

 

                                    if (givenNumber % i == 0) { // condition for non-prime number

                                                flag = true;

                                                break;

                                    }

                        }

 

                        if (!flag)

                                    System.out.println(givenNumber + " is a prime number.");

                        else

                                    System.out.println(givenNumber + " is not a prime number.");

            }

}

Out Put:


2. Check Prime Number using a while loop

PrimeCheckWhileLoop .java


package runnerdev;

 

import java.util.Scanner;

 

public class PrimeCheckWhileLoop {

 

            public static void main(String[] args) {

                        System.out.print("Enter a number to check prime : ");

                        Scanner sc = new Scanner(System.in);

                        int givenNumber = sc.nextInt();

                        int i = 2;

                        boolean flag = false;

                        while (i <= givenNumber / 2) {

 

                                    if (givenNumber % i == 0) {// condition for non-prime number

                                                flag = true;

                                                break;

                                    }

 

                                    ++i;

                        }

 

                        if (!flag)

                                    System.out.println(givenNumber + " is a prime number.");

                        else

                                    System.out.println(givenNumber + " is not a prime number.");

            }

}

Out Put:

Search
Related Articles

Leave a Comment: