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;
}