# Prime Numbers in C++

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

1. ### sopan4uNew Member

Joined:
Jan 28, 2010
Messages:
2
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. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
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. ### sopan4uNew Member

Joined:
Jan 28, 2010
Messages:
2
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. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
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.

Joined:
Dec 19, 2009
Messages:
55