Prime Number Program in C | 2022

Any natural number greater than 1 that can only be divided by 1 is referred to as a prime number.

eg: 2,3,5,7,11 these are the few examples of prime numbers, that can be divided by either 1 or by itself.

Example Program to find the Primer Number in C

#include <stdio.h>

int main() {

  int n, i, isPrimeNumber = 0;
  printf("Enter a positive integer Number: ");
  scanf("%d", &n);

  // 0 and 1 are not prime numbers
  // change isPrimeNumber to 1 for non-prime number
  if (n == 0 || n == 1){
    isPrimeNumber = 1;  
  }
  for (i = 2; i <= n / 2; ++i) {

    // if n is divisible by i, then n is not prime
    // change isPrimeNumber to 1 for non-prime number
    if (n % i == 0) {
      isPrimeNumber = 1;
      break;
    }
  }

  // isPrimeNumber is 0 for prime numbers
  if (isPrimeNumber == 0)
    printf("%d is a prime number.", n);
  else
    printf("%d is not a prime number.", n);

  return 0;
}

Check if 11 is a primer number.

11 is a prime number
11 is a prime number

Check if 9 is a primer number.

9 is not a prime number
9 is not a prime number

Scroll to Top