prime number function

So I need to write a code that prints "true" if a number is prime and "false" if it isn't .. this is the code I have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

  1 #include<iostream>
  2 using namespace std;
  3 int i
  4 bool isPrime(int number)
  5 {
  6   int i;
  7   for (i=2; i<number; i++)
  8   { if(number% i==0)
  9     isPrime=false;
 10   }
 11   if (isPrime==true)
 12     cout << "true" << endl;
 13 }
 14  int main()
 15 { int counter=0;
 16   int current_number=2;
 17   cout<< "First 1000 prime numbers" << endl;
 18   while (counter <1000)
 19   { if (prime_number(current_number)==true)
 20     { cout << current_number << endl;
 21       counter++;
 22     }
 23     current_number++; }
 24     return 0;
 25 }
 26 
~                                                                               
~               
when I try to compile it I get an error saying it expects an initializer before bool
what is that? what would my initializer be? Anything else I could do to fix my code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
  using namespace std;
  int i;
  bool isPrime(int number)
  {
     int i;
     for (i=2; i<number; i++)
     { if(number % i==0)
       return false;
     }
     
     return true; //will return true otherwise
  }
   int main()
  { int counter=0;
    int current_number=2;
    cout<< "First 1000 prime numbers" << endl;
 while (counter <1000)
    { if (isPrime(current_number)==true)
      { cout << current_number << endl;
        counter++;
     }
      current_number++; }
      return 0;
  }


In function bool isPrime, change isPrime=false to return false.

Don't cout << "true" . Return true instead.

In the main function, call isPrime(current_number) to see if its prime or not.
Your initializer error was because you were missing the ; in the line int i in line 3
how can you fix this, when your teacher told you to use "while" not "bool"?
my question : type random number and the answer must be: "This is prime number" or " This isn't prime number"
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>
using namespace std;

int main()
{
   int number;
   int i = 2;

   cout << "Enter number: ";
   cin  >> number;

   while (i < number)
   {
      if (number % i == 0)
      {
         cout << "The number is divisible by " << i
              << " and therefore is not prime";
         break;
      }
      ++i;
   }

   if (i == number)
         cout << "The number is prime.";

   return 0;
}


This code uses a while statement while getting rid of the bool function.
wow!
i got surprised by myself!
many many thanks to you guys, but can you explain me, cause i don't get the point... hahaha ^^
i starts at 2. The while loop is initially true, since i < number. (if number > 2)

if (number % i == 0) means that if number is divisible by i, then it displays that message, and exits the loop.

If number is not divisible by i, then i increments, and the loop starts again with the next number.

If i goes through all numbers up to numberand number still isn't divisible by i, then i ends up equaling number, and the second if statement becomes true.

The reason the second if statement doesn't execute if number isn't prime is because of the break statement inside the loop, which causes the loop to terminate before i is incremented.

Last edited on
Topic archived. No new replies allowed.