Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

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

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students explore these related Databases questions

Question

4. Did you rethink your decision?

Answered: 3 weeks ago