Prime number between two intervals

I am trying to get prime numbers between two intervals. I have a function is_Prime to check if a number is prime or not and returns true or false. I have another function countPrimes to find prime numbers between two intervals using the is_Prime function.

When I input 12 20. The output I get is all the numbers between 12 and 20 instead of the prime numbers only.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
using namespace std;
int n, i;
bool is_Prime(int n){
    if (n < 2){
            return false;
    }
    for (int i = 2; i < (n / 2 + 1); i++){
        if (n % i == 0){
            return false;
        }
    }
    return true;
}

int count_Primes(int x, int y){
while(bool is_Prime = 1)
    is_Prime++;
}

int main(){
int i;
int x;
cin >> x;
int y;
cin >> y;
    for(int i = x+1; i < y; ++i){
        cout << i << endl;
    }
    return 0;
}
Last edited on
while(bool is_Prime = 1)What do you expect from this loop ?
Also you count_Primes doesn't return a value.
Maybe have a look in your textbook about functions and return values.

It would be much easier to use a for loop from x to y and call your is_Prime for each value. If it is a prime increment a counter. At the end of the function return the counter.
Topic archived. No new replies allowed.