0
#include <iostream>
using namespace std;

bool isPrime(int);

int main()
{
    int n;
    cout<<"Enter range to print Prime Numbers\n";
    cin >> n;
    // 1 to 10 - 2,3,5,7
    for(int i = 2;i<=n;i++){
        if(isPrime(i)){
            cout<<i<<", "; //if it is PN
        }
    }
    cout<<endl;
    return 0;
}

bool isPrime(int n) {
    int i;   
    for(i = 2;i<=n/2;i++){  //-n/2
        if(n % i == 0){
            return false;
            break;
        }
    }
    if(i > n/2){
        return true;
    }
}

Here a bool function is used for checking whether an input number is prime or not and returns a value at each conditional statement within it, but still there is an error of not returning a value at all control paths, why???

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Enola
  • 1
  • 1
  • You don't need to `break` after a `return` statement. Also, if you reach the `if (i > n/2)` line, aren't you guaranteed that the condition is true? – paolo Jul 21 '22 at 20:02
  • 1
    I think the compiler did not figure out that i had to be greater than n/2 to exit the for loop however you didn't need to check `if(i > n/2)` just return true; – drescherjm Jul 21 '22 at 20:02
  • 1
    And if your final `if` evaluates to false? What are you returning? The error is very clear on the matter. Better yet, why is that an `if` at all? – sweenish Jul 21 '22 at 20:02

1 Answers1

0

If control reaches the if statement after the loop, then it is guaranteed that i > n/2 is true. However, the compiler does not know this and it sees that no value is returned if the condition is false as there is no else branch and no additional statements. The solution here is to directly return true after the loop; there is no need to check a condition that must be true.

A few additional improvements:

  • using namespace std; should be avoided. Instead, you could write using declarations for only the namespace members you actually need.
  • You can reduce the range of factors you check in the isPrime function by only going up to the square root of the input number.
  • Remove the break statement after return false; since it will never be reached.
#include <iostream>
using std::cout, std::cin, std::endl;

bool isPrime(int);

int main() {
    int n;
    cout << "Enter range to print Prime Numbers\n";
    cin >> n;
    for (int i = 2; i <= n; i++) {
        if (isPrime(i)) {
            cout << i << ", ";
        }
    }
    cout << endl;
    return 0;
}

bool isPrime(int n) {
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • Yeah, but it doesn't allow to use of more than one using declarator, this sequence works fine by declaring each using declarator in different lines. – Enola Jul 21 '22 at 20:53
  • @Muskanahuja It is allowed starting in C++ 17. What version are you using? – Unmitigated Jul 21 '22 at 20:53
  • It is of 17.2.5 – Enola Jul 21 '22 at 20:57
  • @Enola That seems like your compiler version. If you compile with `-std=c++17` (or newer) for GCC, it should work. For Visual Studio, see https://stackoverflow.com/questions/41308933/how-to-enable-c17-compiling-in-visual-studio – Unmitigated Jul 21 '22 at 21:00
  • 1
    @Enola And if that doesn't work, then just use separate `using` statements: `using std::cout; using std::cin; using std::endl;` – Remy Lebeau Jul 22 '22 at 01:45
  • The condition that is always true would be a perfect candidate for an assertion... – Phil1970 Jul 22 '22 at 02:12
  • @RemyLebeau Yes, already But I don't know what's the matter with VSCODE, I have tried every method to update the C++ version in it, but it doesn't work, instead program doesn't execute. – Enola Jul 22 '22 at 17:09