prime numbers

This is a discussion on prime numbers within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main () { int j; int t; int p; int num; cout << ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    45

    prime numbers

    Code:
    
    #include <iostream>
    using namespace std;
    
    int main ()
    
    {
    
       int j;
       int t;
       int p;
       int num;
       cout << " Please enter an even integer greater than 2: ";
       cin >> num;
    
       for ( int i = 2 ; i < num; i++ )
    
    
          if   ( ( i == 2 ) or  ( i % 2  != 0  ) )
    
      {
    
             p = i;
          }
    
       {
             for ( int j = 2; j > p; j++ )
    
                if ( p % j != 0 )
         t = p;
    
             cout << t << endl;
    
          }
    
       return 0;
    
    
      }

    I'm using this program to give me the prime numbers that are less than the number entered, but it's not working?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you asking me? Or is that a statement? Because if you are asking me, I will simply ask why do you use an artificial code block around a for loop?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by xbusterx View Post
    Code:
    
    #include <iostream>
    using namespace std;
    
    int main ()
    
    {
    
       int j;
       int t;
       int p;
       int num;
       cout << " Please enter an even integer greater than 2: ";
       cin >> num;
    
       for ( int i = 2 ; i < num; i++ )
          if   ( ( i == 2 ) or  ( i % 2  != 0  ) )
          {
             p = i;
          }
    
      {
       for ( int j = 2; j > p; j++ )
          if ( p % j != 0 )
             t = p;
       cout << t << endl;
       }
    
       return 0;
    
    
      }

    I'm using this program to give me the prime numbers that are less than the number entered, but it's not working?
    With indentation matching your code, maybe you can see how your program logic is not what you intended it to be.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by master5001 View Post
    Are you asking me? Or is that a statement? Because if you are asking me, I will simply ask why do you use an artificial code block around a for loop?
    Ya I'm asking you what would u do?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include <iostream>
    using namespace std;
    
    int main (void)
    {
      int j;
      int t;
      int p;
      int num;
      cout << " Please enter an even integer greater than 2: ";
      cin >> num;
    
      for ( int i = 2 ; i < num; i++ )
        if   ( ( i == 2 ) or  ( i &#37; 2  != 0  ) )
        {
          p = i;
        }
    
      {
        for ( int j = 2; j > p; j++ )
          if ( p % j != 0 )
            t = p;
        cout << t << endl;
      }
    
      return 0;
    
    
    }

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by master5001 View Post
    Code:
    #include <iostream>
    using namespace std;
    
    int main (void)
    {
      int j;
      int t;
      int p;
      int num;
      cout << " Please enter an even integer greater than 2: ";
      cin >> num;
    
      for ( int i = 2 ; i < num; i++ )
        if   ( ( i == 2 ) or  ( i &#37; 2  != 0  ) )
        {
          p = i;
        }
    
      {
        for ( int j = 2; j > p; j++ )
          if ( p % j != 0 )
            t = p;
        cout << t << endl;
      }
    
      return 0;
    
    
    }
    that doesn't do any this.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's the point he's making -- the braces (in red) don't do anything. So why did you put them there?

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Dude, I just repasted your code so human behings who aren't freaks like me and tabstop can spot the problem from a mile away.

    Are you doing what you are meaning to do, here? I am thinking it does not look like you meant to do this at all.

    Just press CTRL+A then DEL in your IDE. That should relieve the problem using some built in functionality of most modern IDE's... unless you are using eMacs or something.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by master5001 View Post
    Dude, I just repasted your code so human behings who aren't freaks like me and tabstop can spot the problem from a mile away.

    Are you doing what you are meaning to do, here? I am thinking it does not look like you meant to do this at all.

    Just press CTRL+A then DEL in your IDE. That should relieve the problem using some built in functionality of most modern IDE's... unless you are using eMacs or something.
    i'm using emacs.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by xbusterx View Post
    i'm using emacs.
    Ouch. I'm sorry to hear that.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lol its ok. I was just in a bad mood yesterday. I was giving you the shortcut keys to delete the whole source. Which wouldn't be very helpful to you at all.

    I don't mind using eMacs. It makes me feel more computer nerdish when I do (yeah, I know... I am weird like that).

    Back on topic; I am looking at your code and thinking that bracket belongs like this:

    Code:
    #include <iostream>
    using namespace std;
    
    int main (void)
    {
      int j;
      int t;
      int p;
      int num;
      cout << " Please enter an even integer greater than 2: ";
      cin >> num;
    
      for ( int i = 2 ; i < num; i++ )
      {
        if   ( ( i == 2 ) or  ( i &#37; 2  != 0  ) )
        {
          p = i;
        }
    
        for ( int j = 2; j > p; j++ )
          if ( p % j != 0 )
            t = p;
        cout << t << endl;
      }
    
      return 0;
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Replies: 18
    Last Post: 11-04-2005, 01:41 PM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 03:32 PM
  4. Prime numbers
    By Lazy Student in forum C Programming
    Replies: 12
    Last Post: 05-14-2002, 05:53 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21