C Program to check a number is prime or not


In this program, You will learn how to check a number is prime or not in c.


Some list of prime numbers is : 2, 3, 5, 7, 11

Example: How to check a number is prime or not in c.

#include<stdio.h>

int main() {

  int n, i, p = 1;
  printf("Enter a number:");
  scanf("%d", &n);

  for (i = 2; i < n; i++) {
    if (n % i == 0) {
      p = 0;
      break;
    }
  }

  if (p == 1) {
    printf("Number is prime:%d", n);
  } else {
    printf("Number is not prime:%d", n);
  }

  return 0;
}

Output:

Enter a number:7                                                                                                                       
Number is prime:7