943,973 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5326
  • C++ RSS
Oct 28th, 2007
0

Program that finds prime numbers

Expand Post »
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)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main()
  6. {
  7. std::string line;
  8.  
  9. int i, p, x;
  10.  
  11. for(i = 3; i <=100; i++) {
  12.  
  13. for(p=2; p<i; p++)
  14. {
  15. if((i%p) == 0) x = 0;
  16. }
  17. if(x != 0) cout << i << " is prime\n";
  18. x = 1;
  19. }
  20.  
  21.  
  22.  
  23.  
  24. getline(cin,line);
  25. cin.get();
  26.  
  27. return 0;
  28. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
people123 is offline Offline
22 posts
since Oct 2007
Oct 28th, 2007
0

Re: Program that finds prime numbers

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 28th, 2007
0

Re: Program that finds prime numbers

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
people123 is offline Offline
22 posts
since Oct 2007
Oct 28th, 2007
0

Re: Program that finds prime numbers

example. Note placement of braces and spacing. For more information read this by the author of the c++ language.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main()
  6. {
  7. std::string line;
  8.  
  9. int i, p, x;
  10.  
  11. for(i = 3; i <=100; i++)
  12. {
  13. for(p=2; p<i; p++)
  14. {
  15. if((i%p) == 0)
  16. x = 0;
  17. }
  18. if(x != 0)
  19. cout << i << " is prime\n";
  20. x = 1;
  21. }
  22. getline(cin,line);
  23. cin.get();
  24. return 0;
  25. }
Last edited by Ancient Dragon; Oct 28th, 2007 at 9:27 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 29th, 2007
0

Re: Program that finds prime numbers

Click to Expand / Collapse  Quote originally posted by people123 ...
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
Here's a turotial on Formatting C / C++ code
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,744 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: constructor arguments with new
Next Thread in C++ Forum Timeline: string to non const char?





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


Follow us on Twitter


© 2011 DaniWeb® LLC