Finding all the Proper factors of a number


#include <stdio.h>

int main(void) {
  int n, 
    lcv, 
    flag; /* flag initially is 1 and becomes 0 if we determine that n
      is not a prime */
 
  printf("Enter value of N > ");
  scanf("%d", &n);
  for (lcv=2, flag=1; lcv <= (n / 2); lcv++) {
    if ((n % lcv) == 0) {
      if (flag)
 printf("The non-trivial factors of %d are: \n", n);
      flag = 0;
      printf("\t%d\n", lcv);
    }
  }
  if (flag)
    printf("%d is prime\n", n);
}

Post a Comment

Previous Post Next Post