| | |
Program that finds prime numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 22
Reputation:
Solved Threads: 1
So after a day or two of trying i finally found out how to do this, my question is, is there any way to do this that is more efficient without using the prime function?
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { std::string line; int i, p, x; for(i = 3; i <=100; i++) { for(p=2; p<i; p++) { if((i%p) == 0) x = 0; } if(x != 0) cout << i << " is prime\n"; x = 1; } getline(cin,line); cin.get(); return 0; }
The formatting is terrible, maybe you have a mixture of tabs and spaces. I think you have a couple misplaced braces.
Last edited by Ancient Dragon; Oct 28th, 2007 at 8:48 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
example. Note placement of braces and spacing. For more information read this by the author of the c++ language.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { std::string line; int i, p, x; for(i = 3; i <=100; i++) { for(p=2; p<i; p++) { if((i%p) == 0) x = 0; } if(x != 0) cout << i << " is prime\n"; x = 1; } getline(cin,line); cin.get(); return 0; }
Last edited by Ancient Dragon; Oct 28th, 2007 at 9:27 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
how should i improve my formatting, can you please give me an example... i just get lost of what has to be where when im writing
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Print Prime Numbers(using for loop) (C)
- Program to output all prime numbers between 3 and 100 (C++)
- My prime Number generator (Python)
- Finding Prime numbers without using Boolean (C++)
- how do u find prime numbers in an array (C)
- C++ Finding Prime Numbers (C++)
- help with array that determines prime numbers (C)
- prime numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help with array code
- Next Thread: string to non const char?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






