I wrote a very similer program a while back; the difference is that I take user input to give all prime numbers on an interval (so if you say 1 to 100 then it'll list all he prime's from 1 to 100).

I use a different way to "test" if it's prime or not; I set up a loop and user the modulus operator, %, to check if numbers divide into a given number...works really well. Here's the code for the program I wrote for it:

Code:

#include<stdio.h>
/*
Programmer: Matt Lane
Date: 10/18/10
*The purpose of this program is to determine all the prime numbers between to
numbers input by the user
*For example, if one would like to find all the prime numbers between 1 and 50,
the program should outprint this:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
*Note: the program can only display the last 291 prime's on a given interval.
*/
int main ()
{
/*Function prototype*/
int testprime(int);
/*variable to store the return from testprime*/
int resultprime;
/*variable to store the interval to test for prime numbers*/
int a, b;
printf("This program will take an interval [a, b] where a is > b and \n");
printf("all intergers inside the open interval to be prime. \n\n");
printf("All the intergers that are prime will be outprinted at the end \n");
printf("of the program. \n\n");
printf("Please input a value for 'a' in the interval: ");
scanf("%d", &a);
printf("\n\n");
printf("Please input a value for 'b' that is > than 'a' in the interval: ");
scanf("%d", &b);
printf("The following numbers are prime on the interval [%d, %d]: \n",a,b);
while (a <= b)
{
resultprime = testprime(a);
if (resultprime == 0)
printf("\n%d", a);
a++;
}
/*Initilize hold variable*/
char holdprogram;
/*This is to stop the program so that the user can read what
is being displayed*/
fflush(stdin);
printf("\n\n\n\nThe program is completed. \n\n\n");
printf("Press enter to continue. \n\n");
scanf("%c", &holdprogram);
/*Return a value for the int main ()*/
return 0;
}
/*function header*/
int testprime(int inputvalue)
{
/*The variable that will be the counter-control for the loop*/
int i;
/*The variable that will be used to test if a given number divides evenly
into the inputvalue*/
int testforfactor;
/*Set i = inputvalue to ensure the loop runs the proper number of
times but doesn't change the inputvalue's value*/
i = inputvalue;
/*Initilize testforfactor to 0; this is important because if the user
inputs a number < 1 the while loop will never start; so initilizing
this variable to 0 will make the program declare the input is NOT
a prime number since the lowest prime number is > 1 - which will go
through the loop*/
testforfactor = 0;
/*2 is a special prime number because the definition of a prime number
is that the prime number is only divisble by itself and 1; well, do
to the proedure used to calculate that property - 2 will be
considered not prime because it will return a "1" in the while loop
while testforfactor is still = 0*/
if (inputvalue != 2)
{ /*if 1 statement*/
/*This if statment redirects what will be output if the user
inputs a negative number*/
if (inputvalue >= 0)
{ /*if 2 statement*/
/*Inilialize the while loop; i = inputvalue...to test if
a number is prime one must check if numbers BETWEEN the
value itself and 1 (not including itself and 1) will
divide evenly into it*/
while (i > 1)
{ /*while 1 statement*/
/*As previously stated, the program must test all
values between itself and 1; so the loop needs to
lower i so that it is =/= inputvalue but still test
if the new "i" will divide evenly into inputvalue*/
i--;
/*This if statement forces the program out of the loop
once it comes back the final time; because it doesn't
decrease the value of i until AFTER the test for the
while loop occurs...meaning that when i = 2 at the end
of one loop it goes through again but will = 1 and
will ALWAYS divide evenly*/
if (i == 1)
/*So we break out of the while loop before we test
if i = 1*/
break;
/*This is the test for factors of the inputvalue; if
a certain number between inputvalue and 1 divides
evenly, then testforfactor will = 0; if no values
divide evenly then testforfactor will always be
> 0*/
testforfactor = inputvalue % i;
/*If testforfactor = 0, then the number is not prime,
so the program needs to break out of the loop at
this point if that is true*/
if (testforfactor == 0)
/*And the break statement brings it out of the
loop*/
break;
} /*while 2 statemnt*/
/*If the while loop successfully goes through then that
means that testforfactor will never = 0 - which means the
inputvalue is a prime number*/
if (testforfactor != 0)
/*Output that the inputvalue is a prime number because
it passed all the tests to qualify as such*/
inputvalue = 0;
/*If testforfactor DOES = 0, it means that the while loop
hit it's break statment and that the inputvalue is not
a prime number */
else
/*Output that the inputvalue is not a prime number
because it did not pass all the test*/
inputvalue = 1;
} /*if 2 statement*/
/*This else statement corosponds with input value being < 0 */
else
/*Output that the inputvalue was negative and, therefore,
can not be a prime number*/
inputvalue = 1;
} /*if 1 statement*/
/*This else corosponds with input vlaue being = 2; */
else
/*Output that the inputvalue was 2 and, be the definition of a
prime number, it is a prime number*/
inputvalue = 0;
return inputvalue;
}