Program that finds prime numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 22
Reputation: people123 is an unknown quantity at this point 
Solved Threads: 1
people123 people123 is offline Offline
Newbie Poster

Program that finds prime numbers

 
0
  #1
Oct 28th, 2007
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?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Program that finds prime numbers

 
0
  #2
Oct 28th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 22
Reputation: people123 is an unknown quantity at this point 
Solved Threads: 1
people123 people123 is offline Offline
Newbie Poster

Re: Program that finds prime numbers

 
0
  #3
Oct 28th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Program that finds prime numbers

 
0
  #4
Oct 28th, 2007
example. Note placement of braces and spacing. For more information read this by the author of the c++ language.
  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.
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Program that finds prime numbers

 
0
  #5
Oct 29th, 2007
Originally Posted by people123 View Post
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
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC