1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Prime Numbers in C++

Discussion in 'C++' started by sopan4u, Jan 28, 2010.

  1. sopan4u

    sopan4u New Member

    Joined:
    Jan 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    A C++ program to print all the prime numbers less than or equal to the given integer as input.

    Can anybody help me out with this problem??? thanks in advance.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    How far have you got and where are you stuck?
    Do you know how to determine if a number is prime? (if you don't, then you have no chance of writing a program to do it)
    As a starting point you could create a program to input a number then display all the even numbers up less than or equal to that number. Then focus on the prime test, when that's done your program will be complete.

    A mistake beginners often make is to assume they have to write the whole program in one go. Experts don't do that; they write programs a step at a time - often very small steps; I always start with a simple "hello world" program and make sure it builds without errors and runs, before continuing with the next bit. It's amazing how often people complain they're stuck and post the whole code, which can be in some instances over 600 lines long - I can't believe someone would write that much code without even trying to compile it.
     
  3. sopan4u

    sopan4u New Member

    Joined:
    Jan 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    thank you for your reply :) . Yes i know when a number is considered as prime number :) . Can u please help me with the code of this particular program ?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Sure, how far have you got and where are you stuck? Post your code so far.
    If you know how to determine if a number is prime, then that algorithm can be fairly easily converted into code. You could start by just writing down the steps for determining primeness, then modify it if necessary to look more like a program, then start converting each step of the algorithm into one or more lines of code.

    BTW, if "help me" means "write the code for me", the answer is no. You must work through this yourself; it's the only way to learn programming.
     
  5. Deadly Ghos7

    Deadly Ghos7 New Member

    Joined:
    Dec 19, 2009
    Messages:
    55
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Earth
    Home Page:
    do you know how to code in C++? If not, I would recommend you to learn. I hope you know what prime number is and when you know to code C++, you will automatically be able to code your program. And if you are having problem with code if you have written, then post here and we will try to figure out where you made the mistake
     

Share This Page