5-6 Prime

posted Apr 4, 2014, 6:12 AM by Dlovan ismail   [ updated Apr 4, 2014, 6:12 AM ]
/* Write a function that has one argument n ( n is an  integer)
and return true if n is a prime number otherwise return false */


package prime;
import java.util.Scanner;

public class Prime
{
        public static void main(String[] args) 
        {
            Scanner scan=new Scanner(System.in);

            System.out.print("Enter the number : ");
            int n=scan.nextInt();

            if(IsPrime(n))
                System.out.println(n+" is a prime number");
            else
                System.out.println(n+" is not a prime number");

        }

        public static boolean IsPrime(int n)
        {
            boolean flag=true;

            for (int i =2; i < n; i++) 
                if(n%i==0)
                    flag=false;

            return(flag&&n>1);

        }
    
}

ċ
Prime.java
(1k)
Dlovan ismail,
Apr 4, 2014, 6:12 AM
Comments