| | |
Prime Number
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int q; int j; int sum = 0; int p; int num; cout << " Please enter an even integer greater than 2: "; cin >> num; for ( int i = 2; i < num; i++ ) { if ( ( i == 2 ) or ( i % 2 != 0 ) ) p = i; for ( int j = 1; j <= p; j++ ) { if ( p % j == 0 ) { sum += j; } } { if ( sum == p + 1 ) q = p; } cout << q << endl; } return 0; }
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)
#include <iostream> using namespace std; int main () { int q; int j; int sum = 0; int p; int num; cout << " Please enter an even integer greater than 2: "; cin >> num; for ( int i = 2; i < num; i++) { if ( ( i == 2 ) or ( i % 2 != 0 ) ) p = i; for ( int j = 1; j <= p; j++ ) { if ( p % j == 0 ) sum += j; if ( sum == p + 1 ) q = p; } cout << q << endl; } return 0; }
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?
> 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
http://david.tribble.com/text/cdiffs.htm#C99-alt-tok
It's one of the less travelled paths through the C++ standard
![]() |
Similar Threads
- prime number question (C)
- C++ prime number program help (C++)
- Average of prime number between 1 & 100 (C#)
- Print Max Prime Number??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Can somebody please help me write this code
- Next Thread: Pointer to Member Function
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






