Function that Counts Number of Primes

Write a function that counts the number of primes in the range [1-N]. Write the test cases for this function.

Questions by vtpraveen

Showing Answers 1 - 3 of 3 Answers

static int getNumberOfPrime(int N) {
int count = 0;
for (int i=2; i<=N; i++) {
int max = (int)Math.sqrt(i);
boolean prime = true;
for (int j=2; j<=max; j++) {
if (i%j == 0 && i != j) {
prime = false;
break;
}
}
if (prime) {
count++;
System.out.print(i + ",");
}
}
return count;
}

Test case for prime numbers can be :
let the prime no be n
case1: expected o/p (prime no)result
divide the no n by 1 remainder=0 pass
divide the no n by n remainder=0 pass

divide the no n by 2 remainder!=0 pass
.
.
divide the no n by upto n-1 and if remanider not equal zero
then it is a prime no.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions