You are on page 1of 1

Site Title

Home About Contact

1. Write a function named


primeCount with signature int
primeCount(int start, int end);
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
Close and accept
Follow

1. Write a function named primeCount with signature int primeCount(int start, int end);
The function returns the number of primes between start and end inclusive. Recall that
a prime is a positive integer greater than 1 whose only integer factors are 1 and itself.
Examples If start is and end is retur n reason 10 30 6 The primes between 10 and 30
inclusive are 11, 13, 17, 19, 23 and 29 11 29 6 The primes between 11 and 29 inclusive are
11, 13, 17, 19, 23 and 29 20 22 0 20, 21, and 22 are all non-prime 1 1 0 By definition, 1 is
not a prime number 5 5 1 5 is a prime number 6 2 0 start must be less than or equal to
end -10 6 3 primes are greater than 1 and 2, 3, 5 are prime

public class PrimeCount {


public static void main(String[] args) {

// TODO Auto-generated method stub

int result = primeCount(10, 30);

System.out.println(result);

result = primeCount(11, 29);

System.out.println(result);

result = primeCount(20, 22);

System.out.println(result);

result = primeCount(1, 1);

System.out.println(result);

result = primeCount(5, 5);

System.out.println(result);

result = primeCount(6, 2);

System.out.println(result);

result = primeCount(-10, 6);

System.out.println(result);

}
static int primeCount(int start, int end){

int primeCount = 0;

for(int number = start; number <= end; number++){ if(number > 1){

boolean isPrime = true;

for(int divider = 2; divider * 2 <= number; divider++){

if(number % divider == 0){

isPrime = false;

break;

if(isPrime){

primeCount++;

} return primeCount;

}
}

Sponsored Content

 gyanrajrai  Uncategorized  Leave a comment  November 16, 2017 1 Minute

First blog post

This is your very first post. Click the Edit link to modify or delete it, or start a new post.
If you like, use this post to tell readers why you started this blog and what you plan to do
with it.

 gyanrajrai  Uncategorized  Leave a comment  November 16, 2017 1 Minute

Blog at WordPress.com.

You might also like