Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

prime number program in java

// The following method returns a boolean indicating
// if the parameter is a prime number
private boolean isPrimeNaiveIter(int val) {
		if(val == 0 || val == 1)
			return false;
		if(val == 2)
			return true;
        // Look for a divisor other than 1 and val
		for(int div = 2; div*div <= val; div++) {
        	// if found return false
			if(val % div==0)
				return false;
		}
        // No divisors found => return it is a prime number
		return true;
}
Source by beginnersbook.com #
 
PREVIOUS NEXT
Tagged: #prime #number #program #java
ADD COMMENT
Topic
Name
4+4 =