2

I want to write a program that taking some input numbers and then check whether they are prime or not. I wrote it this way:

#include <iostream>

using namespace std;

bool isPrime (int number)
{
    for (int i=2; i<number; i++) 
    {
       if(number % i == 0)
           return false;
       else
           return true;
    }
}

int main()
{
    int y;
    bool z;

    cout << "Enter a positive integer: ";
    cin >> y;

    z = isPrime (y);

    if(z==true)
        cout <<"number is prime" << endl;
    else 
        cout << "number is not prime" << endl;

    system("PAUSE");

    return 0;
}

As, you can see, I wanted to use a function and for loop. But this code is getting just one number. I want to loop the whole input process. How can I make it ?

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
Mahmoud Eidarous
  • 185
  • 1
  • 2
  • 17

6 Answers6

3

Think about what this does:

for (int i=2; i<number; i++) 
{
   if(number % i == 0)
       return false;
   else
       return true;
}

Assume that i is 3 or greater. Then in the first iteration, it checks if number is divisible by two. If it is, then it is not prime, so false is returned. But if it isn't divisible, then function returns true (meaning number should be prime) -- even when it shouldn't (for instance, 9, which is not divisible by 2 nor a prime number).

The solution is to return true only when you know the number can't divide anything (i.e., after the for loop has finished):

for (int i=2; i<number; i++) 
{
   if(number % i == 0)
       return false;
}
return true;

As for the input loop, you could put the input/output part of the program in a while loop:

while(true) {
  cout << "Enter a positive integer: ";
  cin >> y;

  z = isPrime (y);

  if(z==true)
    cout <<"number is prime" << endl;
  else 
    cout << "number is not prime" << endl;
}

Then it will continue to read a number, check if it's a prime and print the result, in a loop. You can put a check (for instance, "quit loop if number is less than 0 (i.e., not valid)")

Frxstrem
  • 38,761
  • 9
  • 79
  • 119
0

Just put a loop around the entire main contents:

int main()
{

    while (true)
    {
        int y;
        bool z;
        cout<<"Enter a positive integer: ";
        cin>>y;
        z= isPrime (y);
        if(z==true)
            cout <<"number is prime" << endl;
        else 
            cout << "number is not prime" << endl;
    }

    system("PAUSE");
    return 0;

}
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Great! Now, I think I can handle with the while loop better than before. It works well for me. Thanks :) – Mahmoud Eidarous May 06 '15 at 22:36
  • Keep in mind, this isn't a perfect set-up when looping. If `cin` goes into a bad state, it will skip asking the user for input and loop endlessly. – Carcigenicate May 06 '15 at 22:38
  • Then, Do you have any advice for a better code, please ? – Mahmoud Eidarous May 06 '15 at 22:42
  • This give good coverage of it. Notice in the askers question he's putting the return from `cin` into an int? Without the `cin.clear()/ignore()` calls, if the user entered a letter, `cin` would fail, quit asking for user input, and the program would loop forever . http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input4 – Carcigenicate May 06 '15 at 22:45
0

Really simply: just wrap the part you want to repeat in a loop:

int main() {

int y;
bool z;
while (true) {
    cout<<"Enter a positive integer: ";
    cin>>y;
    z= isPrime (y);
    if(z==true)
        cout <<"number is prime" << endl;
    else 
        cout << "number is not prime" << endl;
    system("PAUSE");
    }

}

This program will never stop. In C++, while(x) runs a block of code repeatedly as long as x is true. In this case, x is simply true, so it runs the code forever.

One thing to watch out for: I see you indented three lines after your else statement, but you didn't use curly braces. In C++, if you don't use curly braces, if, else, while, for, etc. work on only one line. To make them work on multiple lines, use curly braces like this:

if(z==true) {
    cout <<"number is prime" << endl;
}
else {
    cout << "number is not prime" << endl;
    system("PAUSE");
}
IanPudney
  • 5,941
  • 1
  • 24
  • 39
0

The other answers about simply looping over the input are correct. I would also highly recommend checking some well known algorithms for checking primality. For starters, I would recommend checking the Sieve of Eratosthenes

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Otherwise, you'll run into performance problems when somebody chooses to input a really large number.

apotry
  • 1,856
  • 2
  • 17
  • 23
0

Get rid of your else clause in your function, and move return true; to just outside the for loop. Currently, as soon as your function find one number that your number is divisible by, it will return true. So if your number was 9, your function would quickly discover that it is not divisible by 2 and return true, despite 9 not being a prime number.

Moving return true; to after your for loop would have the effect of making your program loop through all the while numbers less than the number you entered until it either found a number which your number is evenly divisible by, in which case your function would return false, or reached your number, in which case execution leaves the for loop and the function returns true.

bool isPrime(int number)
{
    for (int i = 2; i < number; i++) 
    {
       if (number % i == 0)
           return false;
    }
    return true;
}
  • 1
    About 9.. How would it return true, although the for loop will divide 9 by each number less than it one by one. so it will discover that it is not divisible by 2, and then check 3, and discover that it is divisible by 3, and then will return false. – Mahmoud Eidarous May 06 '15 at 23:17
0

I found this article and it is more efficient in determining if a number is prime or not by using sqrt instead of looping from 2 to N-1.

int isPrime(int n)
{
    int i;

    if (n==2)
        return 1;

    if (n%2==0)
        return 0;

    for (i=3;i<=sqrt(n);i+=2)
        if (n%i==0)
            return 0;

    return 1;
}
Christian Abella
  • 5,747
  • 2
  • 30
  • 42