First of all, you can generally assume, that if you tried to divide nubmer N by all numbers from 2 to sqrt(N), then it is prime(because no number can have prime factor larger then its square).
So, your prime test should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
bool isPrime(int number)
{
bool prime = true;
if(number == 1)
prime = false;
else
{
for(int i = 2; i < number; ++i)
{
if(number % i == 0)
{
prime = false;
break;
}
}
}
}
|
Now back to your question. First of all, you already have two integers - i1 and i2. You don't have to create int a and b and assign i1 or i2 to them. Especially, that a could be ignored and i1 could be used instead, and b isn't used at all.
Also, for loop declaration isn't good. It should be:
1 2
|
for(;a <=i2; ++a)
//code...
|
Yet it is unclear to me what i2 and i1 would be, since you have never commented your code, or used the function.
Output also looks a bit strange to me, but your first if will not work:
|
if(a%!=0)//What is it supposed to do!?
|
And why make a function return an int, when it always returns 0?
Cheers!
PS. I agree with SorinAlex. I would recommend you to:
a) Divide your code into:
- a function that will return bool, and take an integer as an arument, and check if argument is prime(basically the function I wrote above).
- A function that will write to file
- Main function, combining both functions
b)Comment your code - tell us what your code is doing, or is supposed to do. :)
Good luck!