Determining if a number is prime


#include <stdio.h>

int main(void)
{
  int n;
  int i;
  int flag;

  printf("Enter value of N > ");
  scanf("%d", &n);
  flag = 1;
  for (i=2; (i<(n/2)) && flag; ) { /* May be we do not need to test
   values of i greater than the square root of n? */
    if ((n % i) == 0) /* If true n is divisible by i */
      flag = 0;
    else
      i++;
  }
 
  if (flag)
    printf("%d is prime\n", n);
  else
    printf("%d has %d as a factor\n", n, i);
  return 0;
}

Post a Comment

Previous Post Next Post