[go: up one dir, main page]

0% found this document useful (0 votes)
101 views1 page

Write A Function Named Primecount With Signature Int Primecount (Int Start, Int End)

This document contains code for a function that counts the number of prime numbers between two given integers. It includes the function signature, examples of calls to the function with different parameters and expected results, and the implementation of the function that uses a loop to test if each number is prime.

Uploaded by

Dagnachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views1 page

Write A Function Named Primecount With Signature Int Primecount (Int Start, Int End)

This document contains code for a function that counts the number of prime numbers between two given integers. It includes the function signature, examples of calls to the function with different parameters and expected results, and the implementation of the function that uses a loop to test if each number is prime.

Uploaded by

Dagnachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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