Thread: Prime number program not working with odd non-prime numbers

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Prime number program not working with odd non-prime numbers

    I have a program that gets prime numbers that doesn't work. It looks like it works for all prime numbers except odd non-prime ones.
    Here is the code:
    Code:
    #include <iostream>
    using namespace std;
    bool isPrime(int prime) {
         while(true) { 
             int track = 0;
             track++;
             if(prime >= track) {
                    if(prime % 2 == 0) {
                             return true;          
                             }
                             else if(prime % 2 != 0) {
                                  return false;
                                  }
                                  
                                  }
                                  
                                  else {
                                       return 1;
                                       break;
                                       }
                                       }
                                       
                                       }
         int main ( ) {
             int number;
             cout << "Enter number: ";
             cin >> number;
             cout << "" << endl;
             
             if(number == 1) {
                       cout << number << " is prime.";
                       return 0;
                       }
                       if(number == 2) {
                                 cout << number << " is prime.";
                                 return 0;
                                 }
                               
                                           
             else if ( isPrime(number) ) {
             cout << number << " is not prime.";
             }
             else {
             cout << number << " is prime.";
             return 0;
             }
             
             
             }
    Don't post code as a reply, only help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please indent your code properly. It will help you trace through the flow of control in your program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Done.

    Code:
    #include <iostream>
    using namespace std;
    bool isPrime(int prime) {
       while(true) { 
       int track = 0;
       track++;
       if(prime >= track) {
       if(prime % 2 == 0) {
       return true;          
       }
       else if(prime % 2 != 0) {
       return false;
       }
                                  
       }
                                  
       else {
       return 1;
       break;
       }
       }
                                       
       }
       int main ( ) {
       int number;
       cout << "Enter number: ";
       cin >> number;
       cout << "" << endl;
             
       if(number == 1) {
       cout << number << " is prime.";
       return 0;
       }
       if(number == 2) {
       cout << number << " is prime.";
       return 0;
       }
                               
                                           
       else if ( isPrime(number) ) {
       cout << number << " is not prime.";
       }
       else {
       cout << number << " is prime.";
       return 0;
       }
             
             
       }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, that is poorly done. Rather, within the body of a function, or if statement, or loop, or other block of code, you should indent by one level. When that scope ends, you should reduce the indentation by one level. For example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    bool isPrime(int prime) {
        while (true) {
            int track = 0;
            track++;
            if (prime >= track) {
                if (prime % 2 == 0) {
                    return true;
                }
                else if (prime % 2 != 0) {
                    return false;
                }
            }
            else {
                return 1;
                break;
            }
        }
    }
    
    int main() {
        int number;
        cout << "Enter number: ";
        cin >> number;
        cout << "" << endl;
    
        if (number == 1) {
            cout << number << " is prime.";
            return 0;
        }
        if (number == 2) {
            cout << number << " is prime.";
            return 0;
        }
        else if (isPrime(number)) {
            cout << number << " is not prime.";
        }
        else {
            cout << number << " is prime.";
            return 0;
        }
    }
    Now, the first question I pose to you is this: under what conditions should isPrime return true, and under what conditions should isPrime return false? Note that you wrote:
    Code:
    else if (isPrime(number)) {
        cout << number << " is not prime.";
    }
    else {
        cout << number << " is prime.";
        return 0;
    }
    In other words, it sounds like isPrime returns true if the number is not prime, but that would contradict the ordinary understanding of the name of the function.

    Next, doing this in the body of the loop in isPrime:
    Code:
    int track = 0;
    track++;
    has the net effect of:
    Code:
    int track = 1;
    You probably did not intend this.

    Furthermore, this:
    Code:
    if (prime % 2 == 0) {
        return true;
    }
    else if (prime % 2 != 0) {
        return false;
    }
    is effectively the same as:
    Code:
    if (prime % 2 == 0) {
        return true;
    } else {
        return false;
    }
    If you make this amendment to the properly indented version of your code, you will then see that your loop actually only runs for one iteration: every possible branch ends in a return statement.

    Speaking of return statements: you have too many of them in your main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2014, 03:00 PM
  2. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  3. i want to replace the prime numbers by 0 my code is not working
    By wajeeha.javed in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2007, 12:40 AM
  4. Replies: 3
    Last Post: 03-29-2005, 04:24 PM