C Program to check entered number is Prime or not using Function

           A number which is only divisible by 1 or itself is called as Prime number For Example: 7 can only be divided evenly by 1 or 7, so it is a prime number. But 6 can be divided evenly by 1, 2, 3 and 6 so it is NOT a prime number
by using following program user can check the entered number is prime or not.


Program:

//C program to check whether the number is prime or not. By slashmycode.com
#include<stdio.h>
#include<conio.h>
void main()
{
   int n, result;
   printf("Enter an integer to check whether it is prime or not.\n");
   scanf("%d",&n);
   result = check_prime(n);
   if ( result == 1 )
      printf("%d is prime.\n", n);
   else
      printf("%d is not prime.\n", n);
   return 0;
}
int check_prime(int a)
{
   int c;
   for ( c = 2 ; c <= a - 1 ; c++ )
   {
      if ( a%c == 0 )
  return 0;
   }
   if ( c == a )
//C program to check whether the number is prime or not. By slashmycode.com
      return 1;
}


Output:

No comments:

Post a Comment

Feel free to ask us any question regarding this post