Thread: Help

  1. #1
    I need help
    Guest

    Help

    ok heres the problem. I made my program it works fine but when ever I enter a composite number I still get "This is a prime number whats wrong.

    #include <iostream.h>
    #include <cstdio>
    using namespace std;
    int main()
    {
    int x;
    cout<<"Enter a number between 2-100 to see if its Prime or compostite 1 is neither: ";
    cin>>x;
    if(x=2||3||5||7||11||13||17||19||23||29||31||37||4 1||43||47||53||59||61||67||71||73||79||83||89||97)
    {
    cout<<"This is a prime number: ";
    }
    else if(x==1||4||6||8||9||10||12||14||15||16||18||20||2 1||22||24||25||26||27||28||30||32||33||34||35||36| |38||39||40||42||44||45||46||48||49||50||51||52||5 4||55||56||57||58||60||62||63||64||65||66||68||69| |70||72||74||75||76||77||78||80||81||82||84||85||8 6||87||88||90||91||92||93||94||95||96||98||99||100 )
    {
    cout<<"This is a composite number: ";
    }
    getchar();
    return 0;
    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Heh.. quite overkill way of testing for primaility .

    The conditions should look like this.

    if (x == 2 || x == 3 || x == 5 // etc

    However, the easier way to test for primaility is to simply see if any number in the range (2, sqrt(n)] is prime is simply taking n % i (where i is any number in that range). If that operation, n % i is 0 (for hint hint any i the range), then the number is prime.
    Last edited by SilentStrike; 03-23-2002 at 06:03 PM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed