Prime Number

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

Join Date: Oct 2008
Posts: 9
Reputation: koman is an unknown quantity at this point 
Solved Threads: 0
koman koman is offline Offline
Newbie Poster

Prime Number

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


I'm using this program to give me the prime numbers that are less than the number entered, but it's not working?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Prime Number

 
0
  #2
Oct 20th, 2008
The spacing in your code makes it very hard to read:

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

You do realize that the top for-loop starts on line 14 and goes to line 18? Lines 20 - 26 are not part of the for-loop that starts on line 14.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: koman is an unknown quantity at this point 
Solved Threads: 0
koman koman is offline Offline
Newbie Poster

Re: Prime Number

 
0
  #3
Oct 21st, 2008
Originally Posted by VernonDozier View Post
The spacing in your code makes it very hard to read:

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

You do realize that the top for-loop starts on line 14 and goes to line 18? Lines 20 - 26 are not part of the for-loop that starts on line 14.
ok how do I fix that?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Prime Number

 
0
  #4
Oct 21st, 2008
wrap it in braces
{

blah

}



Also, in your if statement, I think you're missing an else?

      if   ( ( i == 2 ) or  ( i % 2  != 0  ) )
      {
         p = i;
      }
      else  // ????
      {
           for ( int j = 2; j > p; j++ )
           {
                if ( p % j != 0 )
                    t = p;
           }
            
            cout << t << endl;
        }
Last edited by chococrack; Oct 21st, 2008 at 1:29 am.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Prime Number

 
0
  #5
Oct 21st, 2008
Originally Posted by koman View Post
ok how do I fix that?

You need brackets around what you want in the for-loop. No brackets means that only the next instruction is part of the loop. Put brackets around what needs to be repeated:

  1. for (/* for-loop code */)
  2. {
  3. // code to be repeated. Inside brackets.
  4. }
  5. // code that is not repeated. Outside the brackets.
Last edited by VernonDozier; Oct 21st, 2008 at 1:24 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: koman is an unknown quantity at this point 
Solved Threads: 0
koman koman is offline Offline
Newbie Poster

Re: Prime Number

 
0
  #6
Oct 21st, 2008
Originally Posted by VernonDozier View Post
You need brackets around what you want in the for-loop. No brackets means that only the next instruction is part of the loop. Put brackets around what needs to be repeated:

  1. for (/* for-loop code */)
  2. {
  3. // code to be repeated. Inside brackets.
  4. }
  5. // code that is not repeated. Outside the brackets.
I've tried chaning the brackets and it still doesn't work.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Prime Number

 
0
  #7
Oct 21st, 2008
Originally Posted by koman View Post
I've tried chaning the brackets and it still doesn't work.
Post what you've tried.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Prime Number

 
0
  #8
Oct 21st, 2008
I am curious ..

how did you get this to compile ?

if ( ( i == 2 ) or ( i % 2 != 0 ) )
There is no keyword called "or" in C++ its denoted by "||"
Last edited by stilllearning; Oct 21st, 2008 at 2:13 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Prime Number

 
1
  #9
Oct 21st, 2008
Also in addition to the braces, your logic is messed up. See the comments in your code.

  1. for ( int i = 2 ; i < num; i++ )
  2. /* you can add a special case for 2, you only need to check for the factors of
  3.   odd numbers which are greater than 2 */
  4. if ( ( i == 2 ) or ( i % 2 != 0 ) )
  5. {
  6. /* you don't really need this placeholder. you can just use "i" */
  7. p = i;
  8. }
  9.  
  10. {
  11. /* this doesn't make sense... you should be checking from 2 upto the odd number.
  12.   j will always be less than p */
  13. for ( int j = 2; j > p; j++ )
  14. /* this doesn't help either, if your odd number is not divisible by the last
  15.   number then it will assume it to be prime ? */
  16. if ( p % j != 0 )
  17. t = p;
  18.  
  19. cout << t << endl;
  20. }

Here is some pseudocode to help

if number is odd
  for i = 2 to number
    if number divisible by i
      haveFactors = true;
      break;
    end if
  end for
  if haveFactors is false, we have a prime number. 
end if
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: koman is an unknown quantity at this point 
Solved Threads: 0
koman koman is offline Offline
Newbie Poster

Re: Prime Number

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


i'm trying to get the ODD numbers but its gives even numbers
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC