Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Prime Numbers in C++ (http://www.go4expert.com/forums/prime-cpp-t20785/)

sopan4u 28Jan2010 10:16

Prime Numbers in C++
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.

xpi0t0s 28Jan2010 12:44

Re: Prime Numbers in C++
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.

sopan4u 28Jan2010 14:31

Re: Prime Numbers in C++
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 ?

xpi0t0s 28Jan2010 14:40

Re: Prime Numbers in C++
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.

Deadly Ghos7 28Jan2010 20:59

Re: Prime Numbers in C++
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

All times are GMT +5.5. The time now is 14:04.