determining prime numbers

hi all....
could you help me...
with this,

Write a program to show prime number from an input number. Range input number is 1-999, if input number is not in that range, you must give an error message to the screen.
Example 1:
Input : 1
Output : 1 is not a prime number

Example 2:
Input : 11
Output : 11 is a prime number

how to make the code????
thank you.......



First upload the code you have...

http://www.cplusplus.com/forum/articles/1295/
One way to do is:

Create a list of every possibly prime number
Check the users input against the list
If users input is a prime number cout << "this is prime"
If users input is not a prime number cout << "this is not prime"
Repeat ???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int userInput;

    cout << "enter a number 1-999 \n";
    cin >> userInput;

    if (userInput <1 || > 999)
    {cout << "oops";}
    
    else
    {
        if (userInput == 2 || userInput == 3   
        || userInput == 5 || userInput == 7)
        {cout << userInput << " is prime";}
        
        else
        {cout << userInput << " is not prime";}
    }


Thats just 1 way of doing this. There are some much better ways. Try to can think of one. Write it out, try the code, if you get stuck show us where and we will try to help.

Good luck to you
Last edited on
Altough your question initially spells out a school a school assignement, to which you have done no prior work/research, check this out, http://en.wikipedia.org/wiki/Primality_test There should be enough material there for you to wirtte a little function using one of the formula there that would take an integer as a parameter and perform the check...

Happy Reading
servalsoft, thanks for that links. Seeing so many post asking this question, I was curious about the code myself. After reading the wiki it was very simple to write.
Topic archived. No new replies allowed.