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.
#include<iostream>
usingnamespace std;
int n, i;
bool is_Prime(int n){
if (n < 2){
returnfalse;
}
for (int i = 2; i < (n / 2 + 1); i++){
if (n % i == 0){
returnfalse;
}
}
returntrue;
}
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;
}
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.