943,516 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1290
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 20th, 2008
0

Prime Number

Expand Post »
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 46
Solved Threads: 0
Newbie Poster
koman is offline Offline
9 posts
since Oct 2008
Oct 20th, 2008
0

Re: Prime Number

The spacing in your code makes it very hard to read:

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Oct 21st, 2008
0

Re: Prime Number

The spacing in your code makes it very hard to read:

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 46
Solved Threads: 0
Newbie Poster
koman is offline Offline
9 posts
since Oct 2008
Oct 21st, 2008
0

Re: Prime Number

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.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 21st, 2008
0

Re: Prime Number

Click to Expand / Collapse  Quote originally posted by koman ...
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:

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Oct 21st, 2008
0

Re: Prime Number

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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 46
Solved Threads: 0
Newbie Poster
koman is offline Offline
9 posts
since Oct 2008
Oct 21st, 2008
0

Re: Prime Number

Click to Expand / Collapse  Quote originally posted by koman ...
I've tried chaning the brackets and it still doesn't work.
Post what you've tried.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Oct 21st, 2008
0

Re: Prime Number

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 21st, 2008
1

Re: Prime Number

Also in addition to the braces, your logic is messed up. See the comments in your code.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 21st, 2008
0

Re: Prime Number

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 46
Solved Threads: 0
Newbie Poster
koman is offline Offline
9 posts
since Oct 2008

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: Can somebody please help me write this code
Next Thread in C++ Forum Timeline: Pointer to Member Function





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


Follow us on Twitter


© 2011 DaniWeb® LLC