943,779 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1290
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 21st, 2008
0

Re: Prime Number

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5.  
  6. {
  7. int q;
  8. int j;
  9. int sum = 0;
  10. int p;
  11. int num;
  12. cout << " Please enter an even integer greater than 2: ";
  13. cin >> num;
  14.  
  15. for ( int i = 2; i < num; i++ )
  16.  
  17. {
  18. if ( ( i == 2 ) or ( i % 2 != 0 ) )
  19.  
  20. p = i;
  21.  
  22. for ( int j = 1; j <= p; j++ )
  23.  
  24. {
  25. if ( p % j == 0 )
  26.  
  27. {
  28. sum += j;
  29.  
  30. }
  31.  
  32. }
  33.  
  34. {
  35. if ( sum == p + 1 )
  36.  
  37. q = p;
  38.  
  39. }
  40. cout << q << endl;
  41.  
  42. }
  43.  
  44. return 0;
  45. }

The brackets on lines 34 and 39 have no effect. The spacing makes it very hard to read. Indent consistently, get rid of the meaningless brackets and the blank lines. Here's your code with better spacing:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5.  
  6. {
  7. int q;
  8. int j;
  9. int sum = 0;
  10. int p;
  11. int num;
  12. cout << " Please enter an even integer greater than 2: ";
  13. cin >> num;
  14.  
  15. for ( int i = 2; i < num; i++)
  16. {
  17. if ( ( i == 2 ) or ( i % 2 != 0 ) )
  18. p = i;
  19.  
  20. for ( int j = 1; j <= p; j++ )
  21. {
  22. if ( p % j == 0 )
  23. sum += j;
  24.  
  25. if ( sum == p + 1 )
  26. q = p;
  27. }
  28.  
  29. cout << q << endl;
  30. }
  31.  
  32. return 0;
  33. }

Reread stilllearning's earlier post about line 17. I was surprised, but this code compiled on Dev C++. I figured "or" would be an error, but apparently Dev C++ didn't mind. Still, best to use || instead of "or".

I don't understand what this alrgorithm has to do with prime numbers. For example, what does sum represent?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Oct 21st, 2008
1

Re: Prime Number

> I was surprised, but this code compiled on Dev C++. I figured "or" would be an error
http://david.tribble.com/text/cdiffs.htm#C99-alt-tok

It's one of the less travelled paths through the C++ standard
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 22nd, 2008
0

Re: Prime Number

Very interesting . I had no idea about this. Thanks Salem.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007

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: Can somebody please help me write this code
Next Thread in C++ Forum Timeline: Pointer to Member Function





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


Follow us on Twitter


© 2011 DaniWeb® LLC