I'm new to C and have a question. This small program is used to test a number that the user enters and see if it is a prime number. Here is the code:
Code:
/* Prime number tester */
#include <stdio.h>
int main(void)
{
int num, i, is_prime;
printf("Enter the number to test: ");
scanf("%d", &num);
/* now test for factors */
is_prime = 1;
for(i=2; i<=num/2; i=i+1)
if((num%i)==0) is_prime = 0;
if(is_prime==1) printf("The number is prime.");
else printf("The number is not prime.");
return 0;
}
My question is about the for statement here. I don't quite understand why it was needed in this program to accomplish the end result or what exactly it did there. I understand for statements in general, just not this particular one. Thanks in advance for your assistance.