Question #24475

Write a program that read 10 integers (in inPrime.txt) and output all the prime numbers in (outPrime.txt)

Expert's answer

#include <iostream>

#include <fstream>

using namespace std;

bool isPrime(int x) {

if (x==1)return false;

else {

boolg=true;

for (inti=2; i*i<=x; i++)

if(x%i==0) {

g=false;

break;

}

return g;

}

}

int main() {

ifstreamin("inPrime.txt");

ofstreamout("outPrime.txt");

for (int i=0;i<10; i++) {

int x;

in>> x;

if (isPrime(x))

out<< x << " ";

}

return 0;

}

#include <fstream>

using namespace std;

bool isPrime(int x) {

if (x==1)return false;

else {

boolg=true;

for (inti=2; i*i<=x; i++)

if(x%i==0) {

g=false;

break;

}

return g;

}

}

int main() {

ifstreamin("inPrime.txt");

ofstreamout("outPrime.txt");

for (int i=0;i<10; i++) {

int x;

in>> x;

if (isPrime(x))

out<< x << " ";

}

return 0;

}

Learn more about our help with Assignments: C++

## Comments

## Leave a comment