# Thread: Is a number Prime or not prime?

1. ## Is a number Prime or not prime?

I need to do this with a couple conditions:
-need to use flags (display_factor) for checking if prime
-need to print out all possibilities with no repeats if not prime
(see below)
333 = 1 x 333
333 = 3 x 111
333 = 9 x 37
333: NOT A PRIME Number
-just print out xxx: PRIME for prime numbers

When the program runs it does not print out the Prime numbers, it only prints out the not-primes. I am reading from a file with around 10 numbers in it. Any help with the PRIME error? Also it runs slow, but that isn't a big deal.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main (void)
{
//declare variables
FILE *fpin;
int n, is_prime, Start, Stop, i, display_factor;

//check to see if file exists and open
if ((fpin=fopen("ints.txt", "r"))==NULL)
{
printf("File ints.txt does not exist");
exit(1);
}

//loops until it reaches the End Of File
while (fscanf(fpin, "%d", &n) != EOF)
{
fscanf(fpin, "%d", &n);
is_prime = 1;

Start = n;
Stop = round(sqrt(n));
display_factor = 1;

for(i = Start; i > Stop; i++)
{
if ((n%i) == 0)
{
is_prime = 0;
if(display_factor == 1)
{
//print out immediate factors
printf("%d = %d x %d\n", n, i, (n/i));
}
}
}
if(is_prime == 1)
{
printf("%d: PRIME Number\n", n);
}
else
{
printf("%d: NOT A PRIME Number\n", n);
}

}

fclose(fpin);

return 0;
}``` 2. I think that the best way to approach this problem is to make a function called "is_prime" that returns either a 1 for yes or 0 for no.

Why are you starting at n? Did you mean to start at 2 and check up to the sqrt(n)? 3. You only need the first one of your fscanf's (the one in the while condition). Having two of them as you do will skip every 2nd number (starting with the first). 4. thanks oogabooga for that, and Click, I'm not good at functions so I'll pass on that but thanks for the suggestion. Now, all numbers it says they are not prime. I am thinking somehow since the first is not prime, the flag is_prime is set to 0 and it doesnt reset itself for the next n... 5. N will almost always be greater that the square root of N.

Tim S.  7. here it is:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main (void)
{
//declare variables
FILE *fpin;
int n, is_prime, Start, Stop, i, display_factor;

//check to see if file exists and open
if ((fpin=fopen("ints.txt", "r"))==NULL)
{
printf("File ints.txt does not exist");
exit(1);
}

//loops until it reaches the End Of File
while (fscanf(fpin, "%d", &n) != EOF)
{
is_prime = 1;

Start = n;
Stop = round(sqrt(n));
display_factor = 1;

for(i = Start; i > Stop; i++)
{
if ((n%i) == 0)
{
is_prime = 0;
if(display_factor == 1)
{
//print out immediate factors
printf("%d = %d x %d\n", n, i, (n/i));
}
}//after for loop

}
if(is_prime == 1)
{
printf("%d: PRIME Number\n", n);
}
else
{
printf("%d: NOT A PRIME Number\n", n);
}

}

fclose(fpin);

return 0;
}``` Code:
`Start = n; // did you mean 2 instead of n?...` 9. Originally Posted by Click_here Code:
`Start = n; // did you mean 2 instead of n?...` Originally Posted by stahta01 N will almost always be greater that the square root of N.

Tim S. 10. well if I try the Start = 2, then it says all numbers are prime. But I understand the logic of starting with 2 11. What is your code when you try Start = 2? 12. Durrh! Now I see what you are saying about n > sqrt(n). I changed this to say i < Stop (changed the sign from > to <). This results in some pretty weird results this is what I get from it:

8: NOT A PRIME Number
25: PRIME Number
101: PRIME Number
5001: NOT A PRIME Number
10213: NOT A PRIME Number
56777: NOT A PRIME Number
765321: NOT A PRIME Number
317971: PRIME Number
1028743: NOT A PRIME Number
23498271: NOT A PRIME Number
987278023: NOT A PRIME Number
1768374681: NOT A PRIME Number 13. laserlight: the start = 2 actually is the only way to run it correctly now with the sign changed from > to < in the if statement.

actually, everything works now except for 25. Also, the output does not print out the "333 = 3 x 111" and such which I need 14. actually I fixed it a bit and here is the output:

8 = 2 x 4
8: NOT A PRIME Number

25: PRIME Number

101: PRIME Number

5001 = 3 x 1667
5001: NOT A PRIME Number

10213 = 7 x 1459
10213: NOT A PRIME Number

56777 = 7 x 8111
56777: NOT A PRIME Number

765321 = 3 x 255107
765321: NOT A PRIME Number

317971: PRIME Number

1028743 = 131 x 7853
1028743: NOT A PRIME Number

23498271 = 3 x 7832757
23498271 = 9 x 2610919
23498271: NOT A PRIME Number

987278023 = 149 x 6626027
987278023 = 761 x 1297343
987278023 = 8707 x 113389
987278023: NOT A PRIME Number

1768374681 = 3 x 589458227
1768374681 = 157 x 11263533
1768374681 = 471 x 3754511
1768374681: NOT A PRIME Number 15. and i just changed this code:

Code:
```        if(is_prime == 1)
{
printf("%d = 1 x %d\n", n, n); //ADDED THIS LINE
printf("%d: PRIME Number\n\n", n);
}``` Popular pages Recent additions flags, homework help, loops, prime, prime numbers 