1

Possible Duplicate:
C++ code for checking for prime numbers not working

The Instructions are: A prime number is a number which is only evenly divisible by 1 and itself. For this assignment you will find which numbers from 1 to n (where n is a user-specified number) are prime. Ask the user for a number, n, greater than 1. Use a loop to iterate on a variable, i, from 2 through n. For each iteration, check all numbers from 2 through i to determine whether the number is prime. If it is prime, print out i and the word "Prime".

This is what i have so far:

 #include<iostream>
 using namespace std;

 int main()
 {
   int i;
   int n;
   cout << "Enter a number greater than 1" << endl;
   cin >> n;

  bool isPrime=true;
  for(i=0; i<=n; i++)
    {
      if (n%i == 0)
        isPrime=false;
    }
  if(isPrime == true)
  cout << i << " is Prime!" << endl;

  return 0;
  }
Community
  • 1
  • 1
  • 7
    Think about what happens when `i == n`, which your loop should reach in its last iteration. – chris Sep 12 '12 at 21:54
  • This smells like a homework question. Homework questions get the homework tag. – Wug Sep 12 '12 at 21:54
  • 3
    Run through a couple of rounds of the loop in your head. You should notice something odd very very quickly. And then again. – Kerrek SB Sep 12 '12 at 21:56
  • 1
    Better yet, run through a couple of rounds in a debugger. – JohnFx Sep 12 '12 at 21:56
  • @LuchianGrigore - that's the **first** failure. There are at least two more. – Pete Becker Sep 12 '12 at 21:57
  • % is the modulo operator, which is the remainder when division is done. Not sure I know what "mod 0" would mean as division by zero tends to be rather undefined in most number systems as I recall. What happens if a user enters "A" for n? – JB King Sep 12 '12 at 22:00
  • You don't really need to test for divisors up until n (even though that's what the requirement asks): http://stackoverflow.com/questions/5811151/why-do-we-check-upto-the-square-root-of-a-prime-number-to-determine-if-it-is-pri – Mihai Todor Sep 12 '12 at 22:05
  • 1
    Refuse to do the assignment on the grounds that a criminally inefficient prime test should at least be creative. – Daniel Fischer Sep 12 '12 at 22:11

3 Answers3

3

I'm not going to answer a suspected homework question with working code, so take this pseudocode instead:

main:
    get number from stdin
    loop from 2 to number:
        if number isprime:
            print number " is prime!"
        increment number
    exit

isprime:
    loop from 2 to number - 1:
        if loop counter evenly divides number
            return false
    return true

You'd have better luck using a function to figure out if a number is prime. It's easier to organize your thoughts this way, and good practice for the future, where you will solve problems where organizing your thoughts will be much harder.

Here's a function overview.

Wug
  • 12,956
  • 4
  • 34
  • 54
2

You could use the Sieve algorithm for this. Look here for algorithm description. Might get extra credit for using a smart solution ;)

Karan Ashar
  • 1,392
  • 1
  • 10
  • 23
0
 bool isPrime(int number){
    bool isPrime = true;
    for(int i = 1; i <= sqrt(number); i++){
        if(number%i == 0)isPrime =  false;
   }
return isPrime;
}