Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include<iostream> #include<fstream> using namespace std; bool isPrime(int); voi

ID: 3619706 • Letter: #

Question

#include<iostream>
#include<fstream>
using namespace std;

bool isPrime(int);

void main()
{
fstream outfile;
outfile.open("primenumbers.txt",ios::out);
for(int number=1; number<=100; number++);
{
if(isPrime (number))
outfile<<number<<endl;
}
outfile.close();
system("pause");
}
bool isPrime (int num)
{
int count=0;
for(int i=2;i<=num/2;i++)
{
if(num%i==0)
count++;
}
if(count>0)
return false;
else
return true;
}
why doesn't this work? I don't think i should have to use the output file either...

Explanation / Answer

please rate - thanks #include<iostream>
#include<fstream>
using namespace std;

bool isPrime(int);

int main()                                  //my compiler needs an int main
{
fstream outfile;
outfile.open("primenumbers.txt",ios::out);
for(int number=1; number<=100; number++) /*you had a ; here so the loop wasn't executing correctly*/
{
if(isPrime (number))
outfile<<number<<endl;
}
outfile.close();
system("pause");
}
bool isPrime (int num)
{
int count=0;
for(int i=2;i<=num/2;i++)
{
if(num%i==0)
count++;
}
if(count>0)
return false;
else
return true;
}