C program to check whether a number is Prime or Not - Primality Checking

 

C program for checking Primality

Prime number 

  1. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.
  2. The property of being prime is called primality

Facts about Prime number

  1. 2 is the only prime number.
  2. If the sum of the number’s digits is multiple of 3, that number can be divided by 3.
  3. No prime number greater than 5 ends with 5.
  4. 0 and 1 are not considered prime numbers.
  5. The largest known prime number (as of December 2020) is 2^82,589,933 − 1, a number that has 24,862,048 digits when written in base 10. It was found via a computer volunteered by Patrick Laroche of the Great Internet Mersenne Prime Search (GIMPS) in 2018.

C program for checking Primality 

    1. #include<stdio.h>
    2. int main()
    3. {
    4.   int num;
    5.   int i;
    6.   int m=0;
    7.   int flag=0;
    8.   printf("Enter a number to check prime or not\n");
    9.   scanf("%d",&num);
    10.   m = num/2;
    11.       for(i = 2; i <= m;i++)
    12.       {
    13.         if(num%i == 0)
    14.           {
    15.              printf("Number is NOT prime\n");
    16.              return 0;
    17.            }
    18.            }
    19.         if(flag==0)
    20.              printf("Number is prime\n");
    21.              return 0;
    22.   }

Post a Comment

0 Comments