## Code to count Prime numbers between a range returns half of the numbers

Some piece of this code is wrong and I can't figure it out. I write the code logically to count Prime Numbers between two given numbers but the code returns half of the range of given numbers. The functions works fine but the logic of while loop is wrong

The code:

#include<stdio.h> int checkPrime(int isItPrime); int main() { printf("Counting Prime Numbers between twos\nEnter two numbers , the First number must be smaller\n"); int num1, num2; scanf("%d%d", &num1, &num2); int count = 0; while(num1<num2) { num1++; if(checkPrime(num1)==1) count++; } printf("%d\n", count); return 0; } int checkPrime(int isItPrime) { int result = 0, j = 2; while(result < isItPrime) { result = (isItPrime%j); j++; if (result==0) return 0; else if (result!=0) return 1; } }

See if this works,I hope it does:

#include<stdio.h> int checkPrime(int n); int main() { printf("Counting Prime Numbers between twos\nEnter two numbers , the First number must be smaller\n"); int num1, num2; scanf("%d%d", &num1, &num2); int count = 0; while(num1<num2) { num1++; if(checkPrime(num1)==1) count++; } printf("%d\n", count); return 0; } int checkPrime(int n) { if (n <= 1) return 0; for (int i = 2; i < n; i++) if (n % i == 0) return 0; return 1; }

**Count Primes,** Count the number of prime numbers less than a non-negative number, n. divisible by any number > n / 2, we can immediately cut the total iterations half by dividing return count; } private boolean isPrime(int num) { if (num <= 1) return false; 2. public: 3. int countPrimes(int n) {. 4. 5. } 6. }; Console. Contribute. Run Code Given an array arr[] of Prime Numbers and a number M, the task is to count the number of elements in the range [1, M] that are divisible by any of the given prime numbers. Examples: Input: arr[] = {2, 3, 5, 7} M = 100

It's OK to `return 0`

the very first time you get `result == 0`

, but don't `return 1`

the very first time you get `result != 0`

. That will cause every odd number to be classified as prime. You have to check the `result`

for *every* value of `j`

up to the square-root of `isItPrime`

. Also: as Brendan pointed out, your `while`

conditional should be testing `j`

, not `result`

. You could say `while(j*j <= isItPrime)`

Also also: note that your current `main()`

logic tests numbers *excluding* `num1`

but *including* `num2`

—verify that that is in fact what you intended.

More generally: be skeptical of your code. Don't just look at the number of (alleged) primes it reports. In initial (debugging) versions of the program, you should be actually printing out each number as it's tested, along with the test result (i.e. whether it thinks each number is prime). If you had done that, you would probably have caught the problems above, or at least narrowed it down.

**Print prime numbers in a given range using C++ STL,** Queries for the difference between the count of composite and prime numbers in a given range. Article Tags : C++ � Mathematical � GE � Prime� Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

OP's `checkPrime()`

incorrectly always return in its first iteration. It need to test more values when `result != 0`

.

int checkPrime(int isItPrime) { int result = 0, j = 2; while(result < isItPrime) { result = (isItPrime%j); j++; if (result==0) return 0; // else if (result!=0) return 1; // problem } return 1; // add }

Fixed OP's algorithm is very slow as it iterates up to `isItPrime`

when iteration only up to `sqrt(isItPrime)`

is needed. Also the return value incorrect with values < 2.

int checkPrime_fix1(int isItPrime) { int result = 0, j = 2; // Avoid sqrt(isItPrime). Floating point has too many subtle problem for an integer task // Avoid j*j <= isItPrime to prevent overflow with large isItPrime while(j <= isItPrime/j) { result = isItPrime%j; j++; if (result==0) return 0; } return isItPrime > 1; }

**Find prime numbers in the first half and second half of an array ,** Given an array arr of size N. The task is to find the prime numbers in the first half ( up to index N/2) and the second half (all the remaining Traverse in the given range Driver Code. int main(). {. int arr[] = { 2, 5, 10, 15, 17, 21, 23 };. int n = sizeof (arr) / sizeof (arr[0]);. // Function call. Print(arr, n);. return 0;. }� I was trying to find the prime numbers within a range (input will be decided by user) and total count of prime numbers. I am able to find the prime numbers but not able to do the part--counting the prime numbers. Can any one help me? here is the program

**Prime Number From 1 to 100 Program in Java,** How to display prime numbers between 1 to 100 using Java Code If the input is 17, half is 8.5, and the loop will iterate through values 2 to 8; If numberToCheck is entirely divisible by another number, we return false, and� In this example, you will learn about C program to display prime numbers between two numbers interval or in a given range by the user with and without using the function. What is prime number ? A prime number is a natural number that is divisible by 1 and itself only. For example: 2, 3, 5, 7 …

**Python Program to Print all the Prime Numbers within a Given Range,** Let the second for loop range from 2 to half of the number (excluding 1 and the Then find the number of divisors using the if statement and increment the count variable Here is source code of the Python Program to print all the prime numbers Prev - Python Program to Find the Gravitational Force Acting Between Two� In this program, the while loop is iterated ( high-low-1) times.. In each iteration, whether low is a prime number or not is checked, and the value of low is incremented by 1 until low is equal to high.

**Calculate the number of primes in a range,** your code does not include 2 as a prime number, it returns the following for you compute the primes between zero and low to check if your� Using the COUNTIFS Function Between Two Values. The COUNTIFS function can show the count of cells that meet your criteria. You can use COUNTIF with criteria using dates, numbers, text and other conditions. Using COUNTIFS you also need to use logical operators (>,<,<>,=). The COUNTIFS function is built to count cells that meet multiple criteria.