Jump to content

is number a prime or not?

- - - - -

  • This topic is locked This topic is locked
16 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi :)

I was trying to write a C++ code to find if the entered number is prime or not. There is a big logic problem in the code which I have failed to fix. At least you can see I tried. Now please help me with it. It would be kind of you.


#include <iostream>

#include <cstdlib>


using namespace std;


int main()


{

    int n, i;

    bool prime = true;


    cout << "Enter the number: ";

    cin >> n;


    for (i=2; i<=(n/2);i++)


    {

        if (n%i != 0)

        prime = true;



        else if (n%i = 0)

        prime = false;


        {

            if (prime = false)

            break;

        }

    }


    if (prime = true)

    cout << "the number is prime" << endl;

    

    else

    cout << "not prime" << endl;


    system("pause");

}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
= is assignment operator and == is comparison operator.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi Dutchman

I don't get it. I replaced many of the "=" with "==" but it didn't work. Could you please help me to fix it?
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
In if statements you want to use comparison rather than assignment.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Any comparisons, actually, e.g. in while loops, do-while and some for loops as well.
sudo rm -rf /

#6
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks a lot, Dutchman, dargueta. It was very helpful. Do you find any syntax or logical errors now? Any suggestions?


#include <iostream>

#include <cstdlib>


using namespace std;


int main()


{

    int n, i;

    bool prime = true;


    cout << "Enter the number: ";

    cin >> n;


    for (i=2; i<=(n/2);i++)


    {

        if (n%i != 0)

        prime = true;



        else if (n%i == 0)

        prime = false;


        {

            if (prime == false)

            break;

        }

    }


    if (prime == true)

    cout << "the number is prime" << endl;


    else

    cout << "not prime" << endl;


    system("pause");

}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
One logical error I see is if user enter 1 it will say that it's a prime number. Number is not a prime unless proven so.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Flying Dutchman said:

One logical error I see is if user enter 1 it will say that it's a prime number. Number is not a prime unless proven so.

Thanks a lot, Dutchman. What about this new code? There is an obvious problem. Although I used the exit function call, it still runs the loop. How do I jump to the end of the program without running the code? Please help me. Thank you.:)


#include <iostream>

#include <cstdlib>


using namespace std;


int main()


{

    int n, i;

    bool prime = true;


    cout << "Enter the number: ";

    cin >> n;


    [B][COLOR="darkorange"]if (n == 1)

    cout << "1 is neither considered a prime nor non-prime" << endl;

    exit;[/COLOR][/B]


    for (i=2; i<=(n/2);i++)


    {

        if (n%i != 0)

        prime = true;



        else if (n%i == 0)

        prime = false;


        {

            if (prime == false)

            break;

        }

    }


    if (prime == true)

    cout << "the number is prime" << endl;


    else

    cout << "not prime" << endl;


    system("pause");

}


Output:

Enter the number: 1

1 is neither considered a prime nor non-prime

[COLOR="red"]the number is prime[/COLOR]

Press any key to continue . . .


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript

if (n == 1)

    cout << "1 is neither considered a prime nor non-prime" << endl;

    exit;


Change to

if (n == 1)

[B][COLOR="RED"]{[/COLOR][/B]

    cout << "1 is neither considered a prime nor non-prime" << endl;

    exit;

[B][COLOR="RED"]}[/COLOR][/B]


Otherwise your program will always exit regardless of the input.
sudo rm -rf /

#10
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

dargueta said:


if (n == 1)

    cout << "1 is neither considered a prime nor non-prime" << endl;

    exit;


Change to

if (n == 1)

[B][COLOR="RED"]{[/COLOR][/B]

    cout << "1 is neither considered a prime nor non-prime" << endl;

    exit;

[B][COLOR="RED"]}[/COLOR][/B]


Otherwise your program will always exit regardless of the input.

Thanks a lot, dargueta. :)

I followed your guideline and used the parentheses. Unfortunately the loop still runs. Perhaps I need to place the part:

if (n == 1)

    {

    cout << "1 is neither considered a prime nor non-prime" << endl;

    exit;

    }


at some place else in the main code. Please guide me. Thanks a lot for your help and time.

Best regards
Jackson


#include <iostream>

#include <cstdlib>


using namespace std;


int main()


{

    int n, i;

    bool prime = true;


    cout << "Enter the number: ";

    cin >> n;


    if (n == 1)

    [COLOR="red"]{[/COLOR]

    cout << "1 is neither considered a prime nor non-prime" << endl;

    exit;

   [COLOR="red"] }[/COLOR]


    for (i=2; i<=(n/2);i++)


    {

        if (n%i != 0)

        prime = true;



        else if (n%i == 0)

        prime = false;


        {

            if (prime == false)

            break;

        }

    }


    if (prime == true)

    cout << "the number is prime" << endl;


    else

    cout << "not prime" << endl;


    system("pause");

}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
I didn't really change your code all that much, but this seems to work.


#include <iostream>

#include <cstdlib>


using namespace std;


int main()

{

    int n, i;

    bool prime = true;


    cout << "Enter the number: ";

    cin >> n;


    if (n == 1)

    {

        cout << "1 is neither considered a prime nor non-prime" << endl;

        return 0;

    }


    for (i=2; i<=(n/2);i++)

    {

        prime = (n % i) != 0;

        if( !prime )

            break;

    }


    if( prime )

        cout << "the number is prime" << endl;

    else

        cout << "not prime" << endl;


    return 0;

}


sudo rm -rf /

#12
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
@jackson6612,
exit;
This is not a function call, you're missing a parenthesis.

exit(1);


A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users