"Prime or not" program

177 views Asked by At

I am stuck on this really simple program in C++, where I let the user know the number she/he have entered is prime or not, but, because of some reason, everything works fine during the first loop but things go fishy during the second. I would be more than happy if anyone could help ?

#include <iostream>
using namespace std;

int main(int argc, const char* argv[])
{
    int number1 = 5;
    int number;
    int a = 0;

    while (number1 == 5)
    {
        int b = 1;
        cout << "Enter your number and we'll tell you if it's prime or not: ";
        cin >> number;

        while (a <= number)
        {
            a++;
            if (number % a == 0)
                b++;
        }

        if (b == 3)
            cout << "Your number is prime" << endl;
        else
            cout << "Your number is not prime" << endl;
    }
}
2

There are 2 answers

6
Ely On

You missed to reinit a to 0 before the inner while.

This makes it work. However, I suggest you take time to learn to code. It does not look educated.

Also your program won't exit. Not sure what your intention is, but you could omit the number1 variable and simply use while(1) (considering your code stands as is; probably you are at the beginning of your development though, so it depends).

#include <iostream>
using namespace std;

int main(int argc, const char* argv[])
{

    int number1 = 5;
    int number;


    int a = 0;

    while (number1 == 5)
    {
        int b = 1;
        cout << "Enter your number and we'll tell you if it's prime or not: ";
        cin >> number;

        a = 0;  <-- Reset to 0 would make it work
        while (a <= number)
        {

            a++;

            if (number % a == 0)
                b++;


        }

        if (b == 3)
            cout << "Your number is prime" << endl;
        else
            cout << "Your number is not prime" << endl;
    }
}

P.S.: You are new to StackOverflow. So you likely take the answer and get away. Please consider accepting the answer. It's a respectful practice when it solved your issue.

0
Vlad from Moscow On

The are several problems with your program.

The first one is that the loop starting with statement

while (number1 == 5)

is infinite because number1 is not changed within the loop.

The second one is that you must always initialize variable a to zero within the loop. And it should be defined also within the loop because it is not used outside the loop. The same is valid for variable number.

Take into account that a number is prime if it is divisble by 1 and itself (except number 1). So I would initially set variable b to zero and compare it with 2. It is more clear than to compare it with 3.

The program can look the following way

#include <iostream>

int main()
{
    while ( true )
    {
        std::cout << "Enter your number and we'll tell you if it's prime or not (0-exit): ";

        unsigned int number = 0;
        std::cin >> number;

        if ( number == 0 ) break;

        unsigned int n = 0;
        unsigned int divisor = 0;

        while ( divisor++ < number )
        {
            if ( number % divisor == 0 ) n++;
        }

        if ( n == 2 )
            std::cout << "Your number is prime" << std::endl;
        else
            std::cout << "Your number is not prime" << std::endl;
    }
}