c++namespacesprimesiostream

i'm writing a cpp program to print all prime numbers between two numbers . Program is running successfully but it is not printing anything


#include <iostream>
using namespace std;
bool isPrime(int num){
    for(int i=2;i<=num;i++){
        if(num%i==0){
            return false;
        }
    }
    return true;
}
int main()
{
    int a,b;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(isPrime(i)){
            cout<<i;
        }
    }
    return 0;
}

Please tell me my mistake, program is running successfully but not printing anything.


Solution

  • for(int i=2;i<num;i++)

    you've used i<=num where the logic should be i<num in your isPrime function. cause all the primes are divided by themselves. but you should not count that while finding primes