C++ 6.23: isPrime Function Use the isPrime function that you wrote in Programmin
ID: 3799126 • Letter: C
Question
C++
6.23: isPrime Function
Use the isPrime function that you wrote in Programming Challenge 21 in a program that reads in an integer that is less than 3001 stores a list of all the prime numbers from 2 through that number in a file name d "PrimeList.txt".
Prompts And Output Labels. No prompts, no labels. After closing the file, the program writes "Prime numbers written to PrimeList.txt." on a line by itself in standard output .
Input Validation If the value read in exceeds 3000, the program silently terminates.
Thank you
Explanation / Answer
Solution:
#include <iostream> //FOR INPUT/OUTPUT OPERATION
#include <fstream> // for file operation
using namespace std;
bool isPrimeNumber(int);
int main()
{
int number=0;
int i;
bool prime;
ofstream outFile;
outFile.open("PrimeList.txt");
while (number == 0 )
{
cin >> number;
}
for(i=2;i<number;i++)
if(isPrimeNumber(i)) // if the number is prime the print it in a file
outFile << i << " ";
cout << "Prime numbers written to PrimeList.txt. ";
outFile.close();
return 0;
}
bool isPrimeNumber(int n) // to check whether the number is prime or not.The output is either true or false
{
int j;
for(j=2;j<n-1;j++)
if(n%j==0)
return false;
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.