Prime Number List in a File?

Write your question here.
Hey guys, I made a program but I don't know if i did it right? When I run it, it doesn't show anything. Here's the question, Use the isPrime function that you wrote in Programming Challenge 22 in a program that stores a list of all the prime numbers from 1 through 100 in a file.
.
#include <iostream>
#include <fstream>
using namespace std;

bool isPrime(int);

int main()
{
int num=0;
int i;
bool prime;

ofstream outFile;
outFile.open("PrimeList.txt");


while (num == 0 )
{
cin >> num;
}


for(i=2;i<num;i++)
if(isPrime(i))
outFile << i << "\n";


cout << "Prime numbers written to PrimeList.txt.\n";
outFile.close();
return 0;
}
bool isPrime(int n)
{
int i;
for(i=2;i<n-1;i++)
if(n%i==0)
return false;
return true;
}
Last edited on
When I run it, it doesn't show anything.

When you run the program, you need to type in the number and press enter. Then the program should run and put the output in PrimeList.txt.
Oh I will try that! Thank you for the responese
Topic archived. No new replies allowed.