By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

coding for the prime number

P: 3
i want u to help me to check my program either it is true or false...
when i run the program, the output will be like:
prime:3 prime:5 and so on. i didn't want the output will be like that.
i just want the output will be like this: (e.g., 3,5,7,11......).
thank you....

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>     
  2. using namespace std;
  3. void prime_num(int);
  4. int main(void)
  5. {
  6.  
  7. int n = 100;     
  8. prime_num(n);
  9.  
  10. }
  11.  
  12. void prime_num( int n)
  13.  
  14. {
  15.         bool isPrime=true;    //The Bool structure provides some basic operations on boolean values.
  16.         for ( int k = 3; k <= n; k++)
  17.         {
  18.                 for ( int r = 2; r <= n; r++)
  19.                 {
  20.                         if ( k!=r && k % r == 0 )
  21.                         {
  22.                           isPrime=false;
  23.                           break;         // to stop looping
  24.                         }
  25.                 }
  26.                 if (isPrime)
  27.                 {
  28.                   cout << "  Prime: " << k <<  "";        // To show the output 
  29.                 }
  30.                 isPrime=true; 
  31.         }
  32.         system("pause");
  33. }
Mar 15 '07 #1
Share this Question
Share on Google+
1 Reply


sicarie
Expert Mod 2.5K+
P: 4,677
If you want it to just say:

Prime: 3, 5, 7, 11 ...

Then move the 'cout << "Prime: "; outside the loop.

As for testing, a compiler will do you better, and you'll get more experience than trying to get us to do and check your homework.
Mar 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.