Check if Number is Prime Algorithm in C


Check if Number is Prime Algorithm  example in C programming :




#include <stdio.h>

main() {

  int n, c = 2;

  printf("Enter a number to check if it is prime\n");
  scanf("%d", &n);

  for (c = 2; c <= n - 1; c++) {

    if (n % c == 0) {

      printf("%d is not prime.\n", n);
      break;

    }

  }

  if (c == n)
    printf("%d is prime.\n", n);

  return 0;
}


OUTPUT
Enter a number to check if it is prime
99
99 is not prime.

Post a Comment

0 Comments