Prime Numbers in C++

sopan4u's Avatar, Join Date: Jan 2010
Newbie Member
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
sopan4u's Avatar, Join Date: Jan 2010
Newbie Member
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 ?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
Deadly Ghos7's Avatar, Join Date: Dec 2009
Contributor
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