How to check for a prime number?

I need to write a program that asks user input of a number,
checks if it is prime, and displays a message if it is prime and saves it to a file.

I also need it to display a message if it is not prime and not save it to a file.

I was able to get the file created but its blank because no matter what number the user enters, it says it is not prime.

How do I get the condition to work to test if the number is prime or not?
What i tried didn't work.

I must then use my isPrime function to store all the prime numbers from 1-100 in a file. Not sure how to go about this.

Should I ask the user to enter all prime numbers 1-100 and store the results in an array? I am not familiar with arrays so if there is an easier way I'd love to know....

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <fstream>
using namespace std;

bool isPrime(int);    //Function prototype


 bool isPrime(int number) //Passes the variable to function isPrime
{
    //Array variables
     const int PRIME_NUMBERS = 25;    //Number of Prime numbers
     int numbers[PRIME_NUMBERS];      //Each user input
     int count;                       //Loop counter

    //Variables
     ofstream outputFile;
     bool status;
     int i=2;

    while(i<=number-1)
    {
        if(number%i==0)
        {
            status=false;
            cout << number << " is not a prime number." << endl;
            break;
        }
        i++;
    }
    if(i==number)
    {
        status=true;

        //Opens file for output
        outputFile.open("PrimeNumbers.txt");

         cout << number << " is a prime number.\n"; //Returns true value

    //Input the prime numbers (1-100)

     for (count = 0; count < PRIME_NUMBERS; count++)
     {

     cout << "\nGreat. Now, please enter all prime numbers (1-100): \n"
          << (count + 1) << ": ";
     cin  >> numbers[count];

     }

     //Display contents of array
     cout << "The numbers you entered are: ";
     for (count = 0; count < PRIME_NUMBERS; count++)
        cout << " " << numbers[count];
        cout << endl;

    //Write contents of array to a file
        outputFile << "List of prime numbers: \n";
    for (count = 0; count < PRIME_NUMBERS; count++)
        outputFile << numbers[count] << endl;

         cout << "This data has been saved to a file.\n";

        //Closes the file.
         outputFile.close();

    }

    return status;
}

 int main()
 {
     //Local variables
     int number;
     bool status;


    //Asks for user input
     cout << "\nPlease enter a number to find if it is prime: \n";
     cin  >> number; //Stores user input

     //Calls isPrime function
     (isPrime(number));

    return 0;
 }

Last edited on
http://en.wikipedia.org/wiki/Primality_test

Your isPrime() should do one thing only: return whether number is a prime. No file operations.

Your main() can loop over numbers, call isPrime with each, and do necessary output.
EDIT: I solved this issue.
Topic archived. No new replies allowed.