Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
Java check whether the number is Prime or not
Sponsered Links
Categories
Sponsered Links

Java check whether the number is Prime or not

Here we are going to check  whether the number entered by the user is prime or not.

import java.util.*;

class CheckPrime{
    static boolean prime(int number){
        boolean isPrime = false;
        int i = (int) Math.ceil(Math.sqrt(number));
        while (i > 1) {
        if((number != i) && (number % i == 0)) {
            isPrime = false;
            break;
            } else if (!isPrime)
            isPrime = true;
            --i;
        }
        return isPrime;
    }
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter number to check: ");
            int num = input.nextInt();
            if (prime(num)) {
                    System.out.println("Number is Prime");
                }
                else{
                    System.out.println("Number is not Prime");
                }
    }
}
 

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved