Friday, September 23, 2011

Check whether a number is Prime or not

//This java program accepts a number from the user
// and checks whether it is a prime number or not.
import java.util.*;
class primeNumber
{
    public static void main(String Args[])
    {
        System.out.println("Enter a number");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int ans=check(n);
        if(ans==1)
        {
            System.out.println("The number is a Prime number");
        }
        else if(ans==0)
        {
            System.out.println("The number is not a Prime number");
        }
    }
   
    public static int check(int a)
    {
        int i,k=1;
        for(i=2;i<a;i++)
        {
            if(a%i==0)
            {
                k=0;
                break;
            }
        }
        return(k);
    }
}
//Author : Mayank Rajoria

No comments:

Post a Comment