Question: C + + program to read a txt file and counts the amount of prime numbers in it . what i have so far is

C++ program to read a txt file and counts the amount of prime numbers in it.what i have so far is incorrect.
#include
using std::cerr;
using std::cout;
using std::endl;
#include
using std::ifstream;
#include
// This method is used to check if a number is prime or no
bool isPrime(int number)
{
if (number <=1)
return false;
for (int i =2; i < number; i++)
{
if (number % i ==0)
{
return false;
}
}
return true;
}
// Driver method to test the code
int main()
{
ifstream input;
int num;
int count =0;
input.open("randomINt.txt");
if (!input)
{
cerr << "File cannot be opened!" << endl;
exit(1);
}
input >> num;
while (!input.eof())
{
if (isPrime(num))
count++;
input >> num;
}
input.close();
cout << "The total number of prime numbers is : "<< count << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!