944,044 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2985
  • C++ RSS
Apr 26th, 2007
0

Finding Prime numbers without using Boolean

Expand Post »
Here is a program which finds prime numbers, but because the compiler i need to use for my class does not use a boolean variable i am having trouble figuring this out. Please help me find how to find prime numbers without using a boolean variable.
  1. #include <iostream>
  2. using namespace std;
  3. void prime_num(int);
  4. int main()
  5. {
  6. cout << " Enter a number and I will generate the prime numbers up to that number: ";
  7. int num = 0;
  8. cin >> num;
  9. prime_num(num);
  10. }
  11.  
  12. void prime_num( int num)
  13. {
  14. bool isPrime=true;
  15. for ( int i = 0; i <= num; i++)
  16. {
  17. for ( int j = 2; j <= num; j++)
  18. {
  19. if ( i!=j && i % j == 0 )
  20. {
  21. isPrime=false;
  22. break;
  23. }
  24. }
  25. if (isPrime)
  26. {
  27. cout <<"Prime:"<< i << endl;
  28. }
  29. isPrime=true;
  30. }
  31. }
Last edited by WaltP; Apr 27th, 2007 at 1:36 am. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnymac5987 is offline Offline
2 posts
since Apr 2007
Apr 26th, 2007
0

Re: Finding Prime numbers without using Boolean

Why not just use an int? Let 0 be false and anything else be true, like in C.

Also, please learn to use [code] and [/code] tags when you post, so we can read your code.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: array, pointers, and strings in C++ please help me asap please.......
Next Thread in C++ Forum Timeline: compiling gcc/c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC