Thread: checking if number is prime

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    checking if number is prime

    Hi

    Do you find the below given code correct? Any suggestions?

    One major problem is that the prime's state true or false is shown with "1" or "0" respectively. How do I get output such as "the number is prime" and "the number is not prime"? Please help me. Thanks a lot.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
        int n, i;
        bool prime;
    
        cout << "Enter the number: ";
        cin >> n;
    
        for (i=2; i<=(n/2);i++)
    
        {
            if (n%i != 0)
            prime = true;
    
    
            else
            prime = false;
    
            {
                if (prime = false)
                break;
            }
        }
    
        cout << prime << endl;
    
        system("pause");
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Have you increased the warning levels for your compiler yet?
    Code:
    $ g++ -Wall foo.cpp
    foo.cpp: In function ‘int main()’:
    foo.cpp:26: warning: suggest parentheses around assignment used as truth value
    Referring to this line
    if (prime = false)

    Also, you only need to go up to sqrt(n), not n/2

    > for (i=2; i<=(n/2);i++)
    A very easy win is to start at 3, and then increase by 2 each time (thus missing all the even numbers)

    > How do I get output such as "the number is prime" and "the number is not prime"?
    Use an if statement - you have several examples already.
    if it's true, print something, or print something else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by jackson6612 View Post
    Hi

    Do you find the below given code correct? Any suggestions?

    One major problem is that the prime's state true or false is shown with "1" or "0" respectively. How do I get output such as "the number is prime" and "the number is not prime"? Please help me. Thanks a lot.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
        int n, i;
        bool prime;
    
        cout << "Enter the number: ";
        cin >> n;
    
        for (i=2; i<=(n/2);i++)
    
        {
            if (n%i != 0)
            prime = true;
    
    
            else
            prime = false;
    
            {
                if (prime = false)
                break;
            }
        }
    
        cout << prime << endl;
    
        system("pause");
    }
    You shouldn't use if statement with single '=' because its makes prime false every time then break. It should be if (prime == false).

    And if you want to print if its prime or not use : if(prime) cout << "It is prime" << endl; else cout << "It is not prime" << endl;
    -

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Salem, kargo.

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Why set it to false and then immediately test if it is false and do the break? Why not just set it to false and break at the same time in the else case?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime number
    By sg786 in forum C# Programming
    Replies: 2
    Last Post: 10-18-2002, 12:27 AM
  2. Prime Number?
    By cprogramnewbie in forum C Programming
    Replies: 8
    Last Post: 03-16-2002, 12:18 AM
  3. prime number algorithm
    By Unregistered in forum C++ Programming
    Replies: 12
    Last Post: 02-22-2002, 11:30 PM
  4. Prime Number problem
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 08:00 PM
  5. How is to check prime number?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-04-2001, 11:36 PM